Weird results when cropping and resizing

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
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Weird results when cropping and resizing

Post by mhulse »

Hello,

I can't seem to figure out the best way to crop and resize to an exact target width and height.

My code:

Code: Select all

convert \
"001.jpg" \
-auto-orient \
-crop 3026x3072+1579+0 \
-gravity Center \
-resize 2622x2660^ \
-density 304x304 \
-quality 100 \
-depth 8 \
-units PixelsPerInch \
-strip \
+profile '*' \
+repage \
cropped-and-resized.jpg
My goal is to crop, and then resize to exactly/proportionlly fit 2622x2660 (hence the use of the carrot ^).

Unfortunately, the cropped results are unpredictable and not 2622x2660.

Also note that some of these images have orientation data straight from camera, that's why I am using auto-orient. Part of me wonders if the orientation info is creating an issue with the crop and resize?

Can anyone see where I am going wrong?

Thanks so much in advance for the help!
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Weird results when cropping and resizing

Post by mhulse »

Ahhh, looks like I am running into issues due to the image's orientation!

The crop is based on the image's orientation before I "fix" it. This would explain why my crops are strange. When I remove -auto-orient, then my image crops great, it's just that it's rotated on it's side!

Now I need to figure out how to fix orientation before determining crop.
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Weird results when cropping and resizing

Post by snibgo »

Try "+repage" after "-auto-orient".
snibgo's IM pages: im.snibgo.com
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Weird results when cropping and resizing

Post by mhulse »

snibgo wrote:Try "+repage" after "-auto-orient".
Awesome! Thank you for the tip!
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Weird results when cropping and resizing

Post by mhulse »

So, after fixing the image's orientation using jhead, this combination of commands ended up working out for me:

Code: Select all

convert \
"uncropped.jpg" \
-auto-orient \
+repage \
-crop 3072x3115+0+9 \
-resize 2622x2660^ \
-extent 2622x2660 \
-density 304x304 \
-quality 100 \
-depth 8 \
-units PixelsPerInch \
-strip \
+profile "*" \
+repage \
"cropped.jpg"
The extent option really helped, as i noticed crop and resize were not creating an exact 2622x2660 image size (I kept getting 2633x2660).

I'm still open to hearing feedback on my command string above if you see anything out of the ordinary. :)

Thanks again for your help @snibgo! :)
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Weird results when cropping and resizing

Post by GeeMack »

mhulse wrote:The extent option really helped, as i noticed crop and resize were not creating an exact 2622x2660 image size (I kept getting 2633x2660).
The resize with the "^" caret will fill that 2622x2660 space the best it can, even if it has to go outside the bounds in one direction or other.
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Weird results when cropping and resizing

Post by mhulse »

GeeMack wrote: The resize with the "^" caret will fill that 2622x2660 space the best it can, even if it has to go outside the bounds in one direction or other.
Ahh, I see! I assumed it would go outside bounds and then crop to the exact size. Thank you for clarification. :)
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Weird results when cropping and resizing

Post by snibgo »

See the docs, http://www.imagemagick.org/script/comma ... p#geometry. The caret "^" does this:
With the ^ operator, one of those dimensions will match the requested size, but the image will likely overflow the dimensions requested to preserve its aspect ratio.
snibgo's IM pages: im.snibgo.com
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Weird results when cropping and resizing

Post by mhulse »

snibgo wrote:See the docs, http://www.imagemagick.org/script/comma ... p#geometry. The caret "^" does this:
With the ^ operator, one of those dimensions will match the requested size, but the image will likely overflow the dimensions requested to preserve its aspect ratio.
Awesome, thank you for the pointer. I totally overlooked that. :)
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Weird results when cropping and resizing

Post by anthony »

A good summary of normal resize cropping techniques is in IM Examples
Thumbnails...
http://www.imagemagick.org/Usage/thumbnails/#fit

A final summery is in
http://www.imagemagick.org/Usage/thumbn ... it_summery
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Weird results when cropping and resizing

Post by mhulse »

anthony wrote:A good summary of normal resize cropping techniques is in IM Examples
Thumbnails...
http://www.imagemagick.org/Usage/thumbnails/#fit

A final summery is in
http://www.imagemagick.org/Usage/thumbn ... it_summery
Thank you Anthony! I really appreciate the help! Reading up on that now. :)
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
Post Reply