Ubuntu终端用ImageMagick裁剪.ps图片并输出为JPG的方法
Got it, let's get that cropping working for your .ps file. The most common issues here are either missing Ghostscript (which ImageMagick depends on to process PostScript files) or not using the right syntax to render the vector PS file properly before cropping. Here's what to do:
Step 1: Make sure Ghostscript is installed
ImageMagick can’t handle PostScript files on its own—it relies on Ghostscript under the hood. If you don’t have it installed, run this command:
sudo apt update && sudo apt install ghostscript
Step 2: Get your crop dimensions and coordinates
First, you need to know exactly which part of the page to crop. Here are two easy ways to get these values:
- Use ImageMagick’s
identifycommand: Run this to pull the full page size and metadata (look for the "Geometry" line to see total width/height):identify -verbose your_file.ps - Use a visual tool like GIMP: Open your .ps file in GIMP (it’ll prompt you for resolution—pick 300 DPI for sharp output). Use the Rectangle Select tool to draw the area you want to crop; the tool’s sidebar will show you the width, height, and top-left coordinates (X/Y) of your selection.
Step 3: Run the correct crop command
The key here is to specify the resolution first (to render the vector PS into a crisp raster image) before applying the crop. Here’s the full command:
convert -density 300 your_file.ps -crop WxH+X+Y +repage cropped_output.png
Let’s break down each part:
-density 300: Renders the PS file at 300 DPI (you can bump this to 600 if you need ultra-sharp output). This is critical—without setting density, you’ll get a low-res, possibly uncropped image since PS is vector-based.your_file.ps: Replace this with your actual .ps filename. If your PS has multiple pages, add[0]to target the first page (e.g.,your_file.ps[0]).-crop WxH+X+Y: Swap W for the width of your cropped area, H for the height, X for the horizontal offset from the top-left corner, and Y for the vertical offset. All values are in pixels (matching the density you set).+repage: Resets the image canvas to the cropped area (optional, but fixes cases where the output still has the original page dimensions with empty space).cropped_output.png: Your final cropped file (change to.jpgif you prefer JPEG format).
Troubleshooting tips
- If the output still matches the original: Double-check your X/Y coordinates and W/H values—they might be outside the page bounds. Try using smaller, safe values first to test.
- If the cropped area is blurry: Increase the
-densityvalue (e.g., 600) for a higher-resolution render. - If you just need to remove whitespace around content: Use
-triminstead of-crop—this automatically crops empty space from all edges:convert -density 300 your_file.ps -trim trimmed_output.png
内容的提问来源于stack exchange,提问作者Hazo

