How can I detect the auto-crop rectangle of image content from the command line?
Asked 1/6/2024
1 views
2 answers
0
I need a command-line way to find the bounding box of the real content in an image, similar to GIMP’s “Crop to Content.” The source is typically a PNG with dark or nearly black margins around the subject, and the edge may be slightly fuzzy rather than perfectly hard. Ideally I want the content rectangle’s width, height, and offset so I can reuse those crop values elsewhere, such as when passing crop parameters to ffmpeg for video processing.
Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0
Photography Stack Exchange contributor
2y ago
2 Answers
2
You can use ImageMagick's -trim operator to obatin a similar result to Gimp's auto-crop, and then use ImageMagick's identify command with a format to extract width and height as numbers:
With your image
$convert TrimSource.png -verbose -trim TrimTarget.png
TrimSource.png=>TrimTarget.png PNG 400x300=>315x201 400x300+29+67 8-bit sRGB 45495B 0.070u 0:00.027
And the output image is indeed 315 × 201 px, and its top right corner is indeed the pixel at (29,67) it the original image.
If you don't care for the output image, you can use a slightly different form to get the output on stdout and then discard it ("BMP" stands for any format (PNG/JPG also work...), just use one that doesn't use inordinate amounts of CPU for an image that will be tossed anyway):
$convert TrimSource.png -verbose -trim BMP:- >/dev/null
TrimSource.png=>- PNG 400x300=>315x201 400x300+29+67 8-bit sRGB 254070B 0.000u 0:00.002
On more recent versions of IM, use magick [convert] instead of just convert. How to extract data from the output is left as an exercise to the reader (depends on your shell language, fairly easy in bash, if that's your target, as in comments I can work it out for you).
Edit: Done for bash:
#! /bin/bash
# Trick command to dump the image to /dev/null and reroute stderr to stdout
out=$( convert $1 -verbose -trim BMP:- 2>&1 >/dev/null )
# Regex to catch the relevant bits from an output like:
# TrimSource.png=>- PNG 400x300=>315x201 400x300+29+67 8-bit sRGB 254070B 0.030u 0:00.010
if [[ $out =~ '=>'([0-9]+)x([0-9]+)' '[0-9]+x[0-9]+'+'([0-9]+)'+'([0-9]+)' ' ]]
then
# If regex matched then the groups in parentheses are in the BASH_REMATCH array
w=${BASH_REMATCH[1]}
h=${BASH_REMATCH[2]}
x=${BASH_REMATCH[3]}
y=${BASH_REMATCH[4]}
printf "Trimmed to %d*%d from (%d,%d)\n" $w $h $x $y
else
printf "Not recognized: %s\n" "$out"
fi
Originally by user75947. Source · Licensed CC BY-SA 4.0
user75947
2y ago
0
Generated from our catalog & community — verify before relying on it.
Yes. A common command-line approach is ImageMagick’s -trim, which auto-crops borders similar to GIMP’s crop-to-content. Its verbose output reports both the cropped size and the offset in the original image.
Example:
convert input.png -verbose -trim output.png
The output includes values like:
400x300=>315x201 400x300+29+67
That means the trimmed content is 315x201, starting at offset +29+67 in the original image.
If you only want the crop rectangle and don’t care about saving the trimmed image, you can still run -trim and inspect the reported geometry. This is useful for deriving width:height:x:y values for tools such as ffmpeg.
If the border is not perfectly black or the edge is soft, an automatic crop tool with color/tolerance controls may work better. One answer suggested XnView MP’s batch convert / automatic crop, or exporting the settings to NConvert for batch processing.
Recommended products
UniqueBot
AI2y ago
Your Answer
Related Questions
Can I automatically reject obviously bad photos using image analysis?
Why is an image file often smaller than pixels × bit depth suggests?
How can I make an exact-size selection in Photoshop?
Can you offset the rotation preview grid in GIMP when straightening an image?
Why does Lightroom's 1:1 crop export a nearly square image instead of exact equal pixel dimensions?