Resize images with ImageMagick to one of several aspects

2

1

I have a collection of images that I process with ImageMagick in a bash script. It loops over the images in a folder. Most of them are in 3:2 format, but some are 4:3, 2:1 and 1:1. However, they are not all perfectly cropped to their aspect ratios, causing layout issues. If I have an image that is 3.01:2 I would like it to be forced into 3:2.

Currently I use the resize command below. Previously I have used scaling and cropping to force-fill 3:2, but that wrongly crops images with other aspect ratios.

convert -resize 300x200

I would like to force the images to the closest of a list of sizes, resizing and cropping to fit. I am fine with defining pixel sizes or (preferably) aspect ratios.

I cobbled together this in the bash script, which gets me something I can test against say a glorious array of if checks. Note that $filename comes from elsewhere:

aspect=$(ffmpeg -i "_originals/$filename"*.jpg 2>&1 | egrep -o "DAR ([0-9]+:[0-9]+)" | cut -f2 -d " " | sed 's/:/ /')
ratio=$(echo $aspect |awk '{printf "%.2f", $1/$2}')
printf "$ratio\n"

Henrik

Posted 2017-06-01T06:49:22.403

Reputation: 121

Use something to read the X/Y dimension, calc the ratio and match it to a selection? – Seth – 2017-06-01T09:15:12.510

Yes, that would roughly be what I am after. But I don't know how I would go about chaining it together. I would guess exiftool to read and then do some calculations? – Henrik – 2017-06-01T11:34:27.613

You could also use identify which also has a MAN page so it might be available. The image dimension isn't really part of the exif information but rather a fundamental of the image structure in most formats I know.

– Seth – 2017-06-01T13:57:28.733

No answers