Page 1 of 1

How to crop only when size is larger than X?

Posted: 2016-12-08T02:35:31-07:00
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

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

Posted: 2016-12-08T05:16:55-07:00
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

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

Posted: 2016-12-08T09:44:22-07:00
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.

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

Posted: 2016-12-08T10:14:30-07:00
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 ...

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

Posted: 2016-12-08T10:23:36-07:00
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.

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

Posted: 2016-12-08T11:45:03-07:00
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