How to resize an image in imagemagick but keep an aspect ratio constant
Asked Answered
C

3

6

I am trying to resize an image (using imagemagick) to keep it's current aspect ratio but fit it into a 4/3 container.

This is the command I have so far:

magick convert ./horse.jpeg -background white -gravity center -extent 4/3 ./hourse_output.jpeg

This is what I'd like: horse. As you can see, the image is "put into" a 4/3 container.

Cestode answered 19/6, 2019 at 17:11 Comment(1)
Related: fit in box of fixed size: stackoverflow.com/questions/18514083/…Halsey
G
5

My error. The aspect ratios such as 4:3 in ImageMagick -extent will only crop and not pad.

See my bash unix script "aspectpad" at http://www.fmwconcepts.com/imagemagick/index.html, which does what you want I think.

Nevertheless, here is a partial solution for how to do it. But this only works for landscape mode input. Also only with ImageMagick 7 due to the use of inline arguments for -extent. You would have to modify it for portrait mode.

Input (aspect 2:1 = 2/1 = 2):

enter image description here

magick barn_2to1.jpg -set option:wd "%[fx:(4/3)>(w/h)?(4/3*h):w]" -set option:ht "%[fx:(4/3)>(w/h)?h:(w/(4/3))]" -gravity center -background black -extent "%[wd]x%[ht]" result.jpg


Output (aspect 4:3 = 4/3 = 1.33):

enter image description here

Note, that I used background of black so that it was visible here. Change to any other color you want.

If the input landscape aspect is larger than 4:3 (4/3), it will pad on top/bottom. If the input landscape aspect is smaller than 4:3, it will pad on left/right.

Input (aspect=1:1 = 1/1 = 1):

enter image description here

magick lena.jpg -set option:wd "%[fx:(4/3)>(w/h)?(4/3*h):w]" -set option:ht "%[fx:(4/3)>(w/h)?h:(w/(4/3))]" -gravity center -background black -extent "%[wd]x%[ht]" result2.jpg


enter image description here

Guendolen answered 19/6, 2019 at 22:7 Comment(3)
Thanks! exactly what I was looking forCestode
Hmmm, on ImageMagick 6.9.7.4, mogrify barn_2to1.jpg -set option:wd "%[fx:(4/3)>(w/h)?(4/3*h):w]" -set option:ht "%[fx:(4/3)>(w/h)?h:(w/(4/3))]" -gravity center -background black -extent "%[wd]x%[ht]" result.jpg failed with: mogrify-im6.q16: invalid argument for option %[wd]x%[ht]': -extent @ error/mogrify.c/MogrifyImageCommand/4819.`.Halsey
mogrify is old and does not likely support -set especially in IM 6. It might or might not work in IM 7, but then you would need magick mogrify. I suggest you use IM 7 magick and not magick mogrifyGuendolen
G
0

Use 4:3 not 4/3. But you have not specified any -resize. In ImageMagick 7, use magick only, not magick convert and not convert. For other tools, use magick identify, magick mogrify, etc. But not for convert. See “Image Geometry” in Anatomy of the Command-line for the 4:3 issue.

Guendolen answered 19/6, 2019 at 17:48 Comment(3)
Thanks for the 4:3 tip! However, the image is stretched now, and not simply "placed" into a 4:3 white container. Any ideas?Cestode
Please post your exact command line and your input image and your ImageMagick version and platform. You still have not shown how you resize your image before trying to enclose it in the 4:3 background.Guendolen
@Guendolen I'm not sure if I want to resize or not, that's kind of what I'm asking. Like in the OP, I want to place any image inside a 4:3 container, either by extending the bottom and then centering (as seen in the OP image) or extending the right and centering.Cestode
G
0

Here is one other way to do it in ImageMagick, if you know the picture is in landscape mode and the image w/h aspect is larger then 4/3. Just pad the top and bottom with plenty of room and then use -extent 4:3 to crop it. This way no computations are needed, so it should work in ImageMagick 6 or 7. If ImageMagick 6, change magick to convert. (If the w/h is less than 4/3 landscape, then pad the left and right.)

Input:

enter image description here

magick barn_2to1.jpg -gravity center -bordercolor black -border 0x100 -background black -extent 4:3 result3.jpg


enter image description here

Guendolen answered 19/6, 2019 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.