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
Image cropping
-
- Posts: 3
- Joined: 2011-02-15T14:02:44-07:00
- Authentication code: 8675308
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Image cropping
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
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
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Image cropping
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!
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...
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.
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
Code: Select all
convert c70f40ac9e.png -crop 702x506+101+145 +repage result.png
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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Image cropping
Anthony,
Your example is very clever. Perhaps you could add it to your pages.
Fred
Your example is very clever. Perhaps you could add it to your pages.
Fred
-
- Posts: 3
- Joined: 2011-02-15T14:02:44-07:00
- Authentication code: 8675308
Re: Image cropping
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
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
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Image cropping
the first trims outside the rectangle mask
The second trims the rectangle mask to leave just the inside of the rectangle.
The second trims the rectangle mask to leave just the inside of the rectangle.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Image cropping
How old is your IM? It may preceded +opaque? Try upgrading!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."!!
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Image cropping
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.
http://www.imagemagick.org/Usage/color_basics/#opaque
Look for the 'warning' note.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/