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
How to crop only when size is larger than X?
-
- 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?
"-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
Re: How to crop only when size is larger than X?
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.snibgo wrote:Code: Select all
convert in.png -gravity Center -crop 512x512+0+0 +repage out.png
- 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?
You need to include the geometry specifiers, the "+0+0" part, with the crop dimensions.elmimmo wrote: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.snibgo wrote:Code: Select all
convert in.png -gravity Center -crop 512x512+0+0 +repage out.png
Code: Select all
... -crop 520x256+0+0 ...
- 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?
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.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
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?
Ouch! Indeed, works!GeeMack wrote:You need to include the geometry specifiers, the "+0+0" part, with the crop dimensions.Code: Select all
... -crop 520x256+0+0 ...
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