Find Bounding Box For B/W Image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
billbell52

Find Bounding Box For B/W Image

Post by billbell52 »

I would like to find the bounding box for a B/W image. Example:

Image

What I want is the most left. right, top and bottom black pixel location. I plan to crop the image to that size or 1 or 2 pixels larger.

I am doing an image compare and I want to make it as fast as possible. I read the docs and examples and did not see anything obvious.

The site I used converted it to jpg. It is actually a bmp file.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Find Bounding Box For B/W Image

Post by fmw42 »

this will get to the top left corner and bottom right corner: ulx=left, uly=top, brx=right, bry=bottom

convert 23hu8ic.jpg -trim 23hu8ic_crop.gif
width=`convert 23hu8ic_crop.gif -format "%w" info:`
height=`convert 23hu8ic_crop.gif -format "%h" info:`
ulx=`convert 23hu8ic_crop.gif -format "%X" info:`
uly=`convert 23hu8ic_crop.gif -format "%Y" info:`
brx=`convert 23hu8ic_crop.gif -format "%[fx:$ulx+$width-1]" info:`
bry=`convert 23hu8ic_crop.gif -format "%[fx:$uly+$height-1]" info:`
echo "ulx=$ulx; uly=$uly; brx=$brx; bry=$bry;"
ulx=+24; uly=+16; brx=143; bry=135;

see -trim and string formats at http://www.imagemagick.org/script/escape.php

This is for unix. If you are on Windows, see http://www.imagemagick.org/Usage/windows/
billbell52

Re: Find Bounding Box For B/W Image

Post by billbell52 »

Thanks for info. Works perfectly. I completely missed the -trim command. Can't say enough for your tremendous support. THANKS!!!
Post Reply