Image cropping

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
hsadek2011
Posts: 3
Joined: 2011-02-15T14:02:44-07:00
Authentication code: 8675308

Image cropping

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image cropping

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Image cropping

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image cropping

Post by fmw42 »

Anthony,

Your example is very clever. Perhaps you could add it to your pages.

Fred
hsadek2011
Posts: 3
Joined: 2011-02-15T14:02:44-07:00
Authentication code: 8675308

Re: Image cropping

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image cropping

Post by fmw42 »

is -trim -trim correct? or should it just be -trim?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Image cropping

Post by anthony »

the first trims outside the rectangle mask
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/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image cropping

Post 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!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Image cropping

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply