Super Rey
Super Rey

Reputation: 377

No-crop canvas increase to 2:1 aspect ratio with ImageMagick

I am in a situation where I have a set of images with variable width and height and I need to ensure that those images comply with an aspect ratio of 2:1.

The only assumption I can make is that those images have an aspect ratio greater than 2 so I need to expand the canvas.

With ImageMagick I can do this:

magick input.jpg -background black -gravity north -extent 2:1 output.jpg

But this crops instead of adding canvas as desired. Is there an option to avoid cropping, i.e. force image padding? Remember I do not know the image resolution beforehand.

Of course with a more complex script I can extract the image dimensions and compute the desired resolution but I am trying to keep things simple if possible.

P.S.: The images comes from a laser scanner and are going to be used in a 360 panorama viewer hence the need to meet an aspect ration without cropping.

Upvotes: 0

Views: 150

Answers (1)

GeeMack
GeeMack

Reputation: 5385

With ImageMagick v7 you can do some inline calculations that will adjust the results of the -extent operation to pad any size input to a 2:1 ratio. See this command as an example...

magick input.png -gravity center -extent "%[fx:max(w,h*2)]x%[fx:max(w/2,h)]" result.png

That will pad any extra space with black. Add -background <somecolor> before the extent operation to change the padding color.

Upvotes: 3

Related Questions