Ubuntu下用ImageMagick裁剪拼接图片 优化Google Vision API调用
Hey there! Let's break down exactly how to handle your task—cropping out those blank photo regions from each user data block, then stitching 30-40 processed images into a single file under 4MB for Google Vision API.
Step 1: Batch Crop the Blank Regions
First, we need to strip away that empty area to the right of the vertical line in each data block. We'll cover two scenarios depending on whether the vertical line's position is consistent or variable.
Scenario 1: Fixed Blank Region Position
If the vertical line sits at a fixed pixel width across all your images (say, 250px from the left), use this batch crop command:
# Create a folder to store processed images mkdir -p processed # Loop through all PNGs, crop to keep only the left 250px, save to processed/ for img in *.png; do convert "$img" -crop 250x%h+0+0 +repage "processed/${img%.png}_cropped.png" done
-crop 250x%h+0+0: Keeps the left 250px (adjust this number to match your vertical line's position) and uses the full height of the original image (%h= original height).+repage: Resets the image's canvas offset so there's no weird spacing later when stitching.
Scenario 2: Variable Blank Region Position
If the vertical line shifts slightly between images, we can auto-detect the right edge of the text area with ImageMagick's trim tool:
mkdir -p processed for img in *.png; do # Auto-detect the width of the non-blank area (allow 5% color fuzz for minor variations) cropped_width=$(convert "$img" -fuzz 5% -trim +repage -format "%w" info:) # Crop the original image to that detected width convert "$img" -crop ${cropped_width}x%h+0+0 +repage "processed/${img%.png}_cropped.png" done
-fuzz 5%: Accounts for tiny color differences (like faint borders) so the trim tool doesn't miss the vertical line.-format "%w" info:: Grabs the width of the trimmed (non-blank) area so we can crop the original image precisely.
Step 2: Stitch Processed Images & Keep File Size Under 4MB
Now we'll stitch 30-40 cropped images into one large file, then compress it to stay under 4MB.
1. Stitch the Images
Use montage (ImageMagick's dedicated tool for image grids) to arrange your processed images. Let's say we want a 10-column grid (adjust -tile to change the layout):
# Stitch all cropped images into a 10-column grid, no spacing between images montage processed/*.png -tile 10x -geometry +0+0 -background white final_raw.png
-tile 10x: Creates a grid with 10 columns; rows will auto-adjust based on how many images you have.-geometry +0+0: Removes any gap between images to save space.-background white: Fills any empty spots in the grid with white (match this to your image's background color if needed).
2. Compress to Stay Under 4MB
The raw stitched image might be too big, so let's compress it with these options:
Option 1: Adjust PNG Compression Quality
This balances image quality and file size. Use -strip to remove unnecessary metadata, then tweak -quality (0-100; lower = smaller file, minor quality loss):
convert final_raw.png -strip -quality 80 final.png
Option 2: Convert to Indexed Color (PNG8)
If your images have limited colors (most text-based images do), converting to 256-color PNG will shrink the file drastically:
convert final_raw.png -strip -colors 256 final.png
Option 3: Split into Multiple Batches
If even after compression you're over 4MB, split your processed images into 2-3 groups and stitch each group separately. This means 2-3 API requests instead of one, but still way fewer than 900!
Verify the Final File Size
Double-check the size with this command:
ls -lh final.png
If it's still over 4MB, try lowering the -quality value or reducing the number of images per stitch batch.
内容的提问来源于stack exchange,提问作者Naveed

