Trimming and keeping the 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?".
Locked
MrGcGamer
Posts: 4
Joined: 2020-03-12T12:18:57-07:00
Authentication code: 1152

Trimming and keeping the aspect ratio

Post by MrGcGamer »

So basically what I want to do is trimming an image but keep its aspect ratio... is that possible using imagemagick and without setting custom values for each image?

Thanks a lot!

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trimming and keeping the aspect ratio

Post by snibgo »

IM has no single operation that does this.

It can still be done: find what a trim would remove using "-format %@", then back-off the trim in whichever direction is needed to maintain the aspect ratio. This probably needs a shell script, though perhaps could be written in a (messy) single IM "magick" command.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trimming and keeping the aspect ratio

Post by fmw42 »

Perhaps use -trim +repage -crop 3:2 +repage, if you want a 3:2 aspect ratio.

See https://imagemagick.org/script/command- ... p#geometry

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trimming and keeping the aspect ratio

Post by snibgo »

Fred's suggestion does a trim, then removes more pixels to get the desired aspect ratio.

I assumed the requirement was to remove fewer pixels than a trim would do. This is more complex, but perhaps isn't needed. @MrGcGamer: please clarify what you want.
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 757
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Trimming and keeping the aspect ratio

Post by GeeMack »

MrGcGamer wrote:
2020-03-14T15:17:12-07:00
So basically what I want to do is trimming an image but keep its aspect ratio... is that possible using imagemagick and without setting custom values for each image?
To give you the best answers we need to know what version of ImageMagick you're using and which OS you're running on. Maybe post some images of exactly what you're trying to accomplish. The solutions depend on a lot of things you haven't told us.

Generally, with ImageMagick the operation "-trim" does a particular thing, which is to shave off rows and columns of same-color pixels from the outer edges of an image. It can't maintain the aspect ratio. If that's what you want, it can probably be done, but not just by trimming, and could get more complicated.

However, there are several ways with IM to chop and shave and otherwise modify the dimensions of the viewport while keeping the aspect ratio, mostly by setting the operations' values as percentages. Here are some examples...

Code: Select all

convert logo: -gravity center -extent 80% result1.png

convert logo: -shave 10% result2.png

convert logo: -gravity northwest -chop 20% result3.png
The first command uses "-gravity center" and "-extent" to reduce the viewport by 10% on every side. That essentially crops out the center 80% of the input image and keeps the input's aspect ratio. The "-gravity" setting can be changed to keep the upper-left, lower-right, left-center, etc. piece instead of the very center.

The second command does exactly what it looks like. It shaves 10% off each of all four sides. The result is the same as the "-extent" operation with "-gravity center", but "-shave N%" affects all four sides regardless of the "-gravity" setting.

The third command uses "-chop", and will remove 20% from the top and left sides of the image. Adjust the "-gravity" setting to make it chop any two adjacent sides. This also maintains the input aspect ratio, but it only works on two adjacent sides at a time. It can't keep a piece from the center.

Locked