Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop the rectangle region from the image using imagemagick

I have an image. I need to crop the rectangle region from the image. This rectangle region is identified by black color border. Inside the rectangle is what I need. Is it possible to crop the black color bordered rectangle region in imagemagick? I know it could be possible using crop command by providing offset(-crop WxH+X+Y) of the rectangle region. But I want to crop the rectangle region without manually measuring the upper left and lower right corners of the rectangle. Is it possible to crop the rectangle region using black color border alone...???

like image 926
Thirumurthy Avatar asked Jan 22 '26 02:01

Thirumurthy


1 Answers

How about something like this?

convert source.jpg -fuzz 10% -bordercolor black -border 1x1 -trim +repage dest.jpg

You may have to play around with the 'fuzz' percentage. The reason you need the fuzz option is that without it trim will only trim pixels that are exactly black - with JPEGs this is unlikely to be the case.

All this is explained on this page: http://www.imagemagick.org/Usage/crop/#trim

This solution will only work if the black border goes right up to the edges of the image. If this is not the case then I don't think you'll be able to do what you need to with IM without examining the image (e.g. pixel by pixel) programmatically.

like image 51
Martin Wilson Avatar answered Jan 25 '26 17:01

Martin Wilson