How to crop by aspect ratio?

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
etrader
Posts: 13
Joined: 2012-09-29T06:53:04-07:00
Authentication code: 67789

How to crop by aspect ratio?

Post 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?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to crop by aspect ratio?

Post 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.
etrader
Posts: 13
Joined: 2012-09-29T06:53:04-07:00
Authentication code: 67789

Re: How to crop by aspect ratio?

Post by etrader »

It works beautifully.

Anyhow, my version is ImageMagick 7.0.8-34 on Ubuntu.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to crop by aspect ratio?

Post 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?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to crop by aspect ratio?

Post 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.
Post Reply