Using -crop on jpeg images

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
mistergoomba

Using -crop on jpeg images

Post by mistergoomba »

These lines produce different results:

Code: Select all

convert halloween_category.gif -crop 60x60-10+10\! halloween_cropped.gif
convert amber.jpg -crop 60x60-10+10\! amber_cropped.jpg
halloween_cropped.gif comes out as a 60x60 image with 10 pixels of whitespace on the left. amber_cropped.jpg comes out as a 50x60 image.

The intended behavior is that both images would come out as a 60x60 image, however it only seems to work properly on gif images. How can I fix this?

Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using -crop on jpeg images

Post by fmw42 »

don't know if this will help, but try adding +repage after your crop

http://www.imagemagick.org/Usage/crop/#crop_repage

The problem may be that jpg does not support virtual canvas and so negative offsets may not work.
mistergoomba

Re: Using -crop on jpeg images

Post by mistergoomba »

Yea, using +repage didn't seem to help. Anything else?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using -crop on jpeg images

Post by fmw42 »

I suspect that you cannot use negative offsets on jpg because it does not support virtual canvase

so just crop the part you want without negative offsets (50x60) and then pad 10 pixels of white on the left using splice

see http://www.imagemagick.org/Usage/crop/#splice
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Using -crop on jpeg images

Post by anthony »

mistergoomba wrote:These lines produce different results:

Code: Select all

convert halloween_category.gif -crop 60x60-10+10\! halloween_cropped.gif
convert amber.jpg -crop 60x60-10+10\! amber_cropped.jpg
halloween_cropped.gif comes out as a 60x60 image with 10 pixels of whitespace on the left. amber_cropped.jpg comes out as a 50x60 image.
Actually internally they BOTH come out as 50x60 pixel images (actual number of pixels in the actual image cropped. That was because the -10 means the crop slightly missed the image, so the image does not 'fill' the 60x60 pixel area.

The '!' caused the crop to generate a , 'viewport' crop which cause the cropped image to be set on a virtual canvas the same size as the crop area at the position given. That is 60x60, and causing the 50x60 pixel actual image to be located at a +10+0 on the 60x60 virtual canvas.

However the difference in the results is that the JPEG file format does not handle virtualk canvas information at all. so you just get the 50x60 actual image and nothing else.

GIF file format however can save the virtual canvas information so the 'white' area is just the 'empty part of the GIF's virtual canvas that did not have the image overlaid. The white would be the white of the PAGE BACKGROUND not from the image.

If you want to 'fill out' the missing area, add -flatten to the command.

Code: Select all

convert halloween_category.gif -crop 60x60-10+10\! -background white -flatten halloween_cropped.gif
convert amber.jpg -crop 60x60-10+10\! -background white -flatten amber_cropped.jpg
That will replace the virtual canvas with a real image (of the current background color) and overlay the image at +10+0. That is it will 'fill in' the pixels missing from the 'viewport' which is no longer needed afterwards.

As the virtual canvas has been 'filled' ALL image file formats will handle it the same way as their is no virtual information to cause problems.

See Virtual Canvas info and the 'page' options that control it.
http://www.imagemagick.org/Usage/basics/#page
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply