How to crop only when size is larger than X?

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
elmimmo
Posts: 26
Joined: 2011-02-02T05:42:42-07:00
Authentication code: 8675308

How to crop only when size is larger than X?

Post by elmimmo »

How can I crop an image to a maximum width × height (512×512), ideally also specifying -gravity center. If any of the image's dimension is lower than the specified size, it should be left as is. I.e:

600×600 → 512×512
200×600 → 200×512
600×200 → 512×200
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to crop only when size is larger than X?

Post by snibgo »

"-crop" does what I think you want. Use it with "-gravity" and "+repage", if you want, eg:

Code: Select all

convert in.png -gravity Center -crop 512x512+0+0 +repage out.png
snibgo's IM pages: im.snibgo.com
elmimmo
Posts: 26
Joined: 2011-02-02T05:42:42-07:00
Authentication code: 8675308

Re: How to crop only when size is larger than X?

Post by elmimmo »

snibgo wrote:

Code: Select all

convert in.png -gravity Center -crop 512x512+0+0 +repage out.png
I tried with an image 520×256, but -gravity seems not to be doing anything: cropping starts from the left with both -gravity center and -gravity east, with the a first output being 520×256. Then it outputs an additional file pertaining the image area beyond 512px. I expected a single image, and cropping to happen at both sides.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to crop only when size is larger than X?

Post by GeeMack »

elmimmo wrote:
snibgo wrote:

Code: Select all

convert in.png -gravity Center -crop 512x512+0+0 +repage out.png
I tried with an image 520×256, but -gravity seems not to be doing anything: cropping starts from the left with both -gravity center and -gravity east, with the a first output being 520×256. Then it outputs an additional file pertaining the image area beyond 512px. I expected a single image, and cropping to happen at both sides.
You need to include the geometry specifiers, the "+0+0" part, with the crop dimensions.

Code: Select all

... -crop 520x256+0+0 ...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to crop only when size is larger than X?

Post by fmw42 »

elmimmo wrote:How can I crop an image to a maximum width × height (512×512), ideally also specifying -gravity center. If any of the image's dimension is lower than the specified size, it should be left as is. I.e:

600×600 → 512×512
200×600 → 200×512
600×200 → 512×200
Snibgo's solution should work. If the image is smaller in one dimension, you will just get the input image as the output. If it is bigger in one dimension and smaller in the other, it will crop the bigger dimension only.

Code: Select all

convert in.png -gravity Center -crop 512x512+0+0 +repage out.png
What is your IM version and platform? Please always provide that information, since syntax may differ.
elmimmo
Posts: 26
Joined: 2011-02-02T05:42:42-07:00
Authentication code: 8675308

Re: How to crop only when size is larger than X?

Post by elmimmo »

GeeMack wrote:You need to include the geometry specifiers, the "+0+0" part, with the crop dimensions.

Code: Select all

... -crop 520x256+0+0 ...
Ouch! Indeed, works!

I am using this version:

Code: Select all

$ convert --version
Version: ImageMagick 6.9.5-5 Q16 x86_64 2016-08-07 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules 
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib
Post Reply