Crop with specific aspect ratio (feature suggestion)

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
bfg9000
Posts: 1
Joined: 2013-02-26T03:40:16-07:00
Authentication code: 6789

Crop with specific aspect ratio (feature suggestion)

Post by bfg9000 »

Hi.
I miss a feature of cropping with a specific aspect ratio (or I've failed finding it in the docs)

It could work like this

Code: Select all

convert in.png -gravity center -crop 4:3 out.png
and the result would be the file in.png is cropped to a ratio 4:3 (keeping as much as possible of the original file).

Looking forward to seeing this in the next version :D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop with specific aspect ratio (feature suggestion)

Post by fmw42 »

you can do that now by computing the Width and Height to satisfy your aspect ratio and then using just crop WxH+0+0 +repage.

The issue you must consider which contributes to the coding is which dimension to use to compute the aspect ratio so that you can crop and not need to pad. Thus you may need to compute the aspect ratio of the input image and compare it to the desired aspect ratio.

for unix see my scripts, aspect and aspectpad, thought they do not do exactly what you want.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Crop with specific aspect ratio (feature suggestion)

Post by magick »

Try -crop 100%x75%+0+0.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Crop with specific aspect ratio (feature suggestion)

Post by anthony »

I would have though that only works for square input images...

In IMv7 you can use %[fx:...] expressions directly in -crop, or better still -extent (which can crop or pad an image to fit the calculated size).

In IMv6 you can fudge it using a distort viewport...
For example see IM Examples, Centered Square Crop
http://www.imagemagick.org/Usage/distor ... red_square
This is for the largest 1:1 crop you can get from an image. But should be able to calculate a largest 4:3 ratio crop with minimal changes.

Anyone like to give it a go... Results can be added to the above area of IM examples with your name.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Crop with specific aspect ratio (feature suggestion)

Post by GreenKoopa »

I was going to create this example, but after some thought it doesn't seem too useful. First, the OP sets gravity, which -distort doesn't use. Your square example assumes a gravity of center. Second, the solution is less well defined when not a square. If the input image is 90x100 and 4:3 is requested, what is the output dimensions desired? 90x68 is a 4:3.02 ratio and 90x67 is a 4:2.98 ratio. Or is a 88x66 output better? Maybe the user didn't intend to impose an orientation, so 75x100 is best?

Your square example shows the concept. A user could easily adapt that to their well-defined need.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop with specific aspect ratio (feature suggestion)

Post by fmw42 »

I have a bash unix script, aspectpad, that pads to the desired aspect ratio. I also have a script, aspect, that will either crop or pad, but it includes a resize. So you would need to specify the aspect widthxheight so as not to resize. See the link below. Neither do exactly what you want, but may be close enough for your needs.
Post Reply