Page 1 of 1
Image cropping
Posted: 2011-03-03T14:11:02-07:00
by hsadek2011
I have an image that I need to crop along a white rectangle. Inside the rectangle is what I need. Meaning I need to remove the portion from the border to the white rectangle. Is it possible in imagemagick?
Kindly find below an example for the image
http://www.freeimagehosting.net/uploads/c70f40ac9e.png
Thanks,
-Hussein
Re: Image cropping
Posted: 2011-03-03T14:53:26-07:00
by fmw42
Yes, but you have to manually measure the upper left and lower right corners where you want to crop and then change that to the format -crop WxH+X+Y
where X and Y are the upper left corners where you want to crop and WxH is the size of the crop (difference between upper left and lower right corners)
see
http://www.imagemagick.org/Usage/crop/#crop
An automated way is:
If you want the white border:
convert c70f40ac9e.png -fuzz 60% -trim +repage c70f40ac9e_tmp2.png
If you don't want the white border:
convert c70f40ac9e.png -fuzz 60% -trim +repage -shave 1x1 +repage c70f40ac9e_tmp3.png
but the fuzz value may be image dependent and will require some trial and error (as I did to get at 60%; note I started at 50% first, but it was not quite adequate)
see
http://www.imagemagick.org/Usage/crop/#trim
Re: Image cropping
Posted: 2011-03-03T16:55:36-07:00
by anthony
if you can guarantee that the white rectangle is unique. I would first make a mask from 'white' colors.
then trim on that mask to get the bounds. To remove the white rectangle itself from the results.
you can either... subtract 2 from size and add 1, shave the trim result, or simply just trim twice!
Code: Select all
convert c70f40ac9e.png -fill black +opaque white -trim -trim -format '%wx%h%O' info:
702x506+101+145
That output is the geometry (bounds) of the insides of the rectangle, and can now be used as the crop argument against the original image...
Code: Select all
convert c70f40ac9e.png -crop 702x506+101+145 +repage result.png
In many ways this is just a variation of 'trimming a noisy image'
http://www.imagemagick.org/Usage/crop/#trim_blur
A two stage crop/trim of an image, except that instead of blurring and fuzz trimming, we create a mask of the white white rectangle.
Re: Image cropping
Posted: 2011-03-03T18:01:01-07:00
by fmw42
Anthony,
Your example is very clever. Perhaps you could add it to your pages.
Fred
Re: Image cropping
Posted: 2011-03-07T15:23:39-07:00
by hsadek2011
Thanks Fred, your line worked fine.
Thanks Anthony too for your help, but when I run the first line it gives me a message says "convert: unable to open image `white': No such file or directory."!!
The second line worked fine, but I wonder what is the methodology you use to come up with the right cut off pixels?? and assume if I don't have a white line, I may have 4 points representing the 4 coordinates.
Thanks,
-Hussein
Re: Image cropping
Posted: 2011-03-07T19:46:23-07:00
by fmw42
is -trim -trim correct? or should it just be -trim?
Re: Image cropping
Posted: 2011-03-07T21:24:18-07:00
by anthony
the first trims outside the rectangle mask
The second trims the rectangle mask to leave just the inside of the rectangle.
Re: Image cropping
Posted: 2011-03-07T21:46:45-07:00
by fmw42
Thanks Anthony too for your help, but when I run the first line it gives me a message says "convert: unable to open image `white': No such file or directory."!!
How old is your IM? It may preceded +opaque? Try upgrading!
Re: Image cropping
Posted: 2011-03-07T22:00:07-07:00
by anthony
An alturnative for older IM's to +opaque is provided in IM Examples
http://www.imagemagick.org/Usage/color_basics/#opaque
Look for the 'warning' note.