Page 1 of 1

How to crop by aspect ratio?

Posted: 2019-04-09T00:43:44-07:00
by etrader
For resizing, we can tell IM to resize if an image is larger than a given size. Is it possible for crop (gravity) too?

I want to prepare images for instagram, which apparently has limitations for aspect ratio:

Landscape: 1.91:1
Portrait: 4:5

Can I tell IM to crop only if the image is beyond these aspect ratios?

Re: How to crop by aspect ratio?

Posted: 2019-04-09T07:25:22-07:00
by GeeMack
etrader wrote: 2019-04-09T00:43:44-07:00Landscape: 1.91:1
Portrait: 4:5

Can I tell IM to crop only if the image is beyond these aspect ratios?
You should always include the version of IM you're using and which platform you're running it on.

With most recent versions you can crop to a particular aspect ratio using "-extent" like this...

Code: Select all

magick input.png -gravity center -extent 1.91:1 result.png
To get a more thorough answer you'll have to provide more information.

Re: How to crop by aspect ratio?

Posted: 2019-04-09T08:37:49-07:00
by etrader
It works beautifully.

Anyhow, my version is ImageMagick 7.0.8-34 on Ubuntu.

Re: How to crop by aspect ratio?

Posted: 2019-04-09T08:54:40-07:00
by snibgo
Thanks, GeeMack. I though there was an operation for this, but forgot what it was.

@Fred: Can we update http://www.imagemagick.org/script/comma ... php#extent to note this syntax, please?

Re: How to crop by aspect ratio?

Posted: 2019-04-09T09:24:05-07:00
by fmw42
Can I tell IM to crop only if the image is beyond these aspect ratios?
Not that I know. You will have to test the aspect ratios before hand.


You can also do

Code: Select all

magick input.png -gravity center -crop 1.91:1+0+0 +repage result.png
But neither version respects >

Code: Select all

magick input.png -gravity center -crop 1.91:1+0+0\> +repage result.png
If I start with a square image and use the \>, the result is still 1.91:1.