如何在imagemagick中调整图像大小但保持宽高比不变 [英] How to resize an image in imagemagick but keep an aspect ratio constant
问题描述
我正在尝试使用imagemagick调整图像的大小以保持其当前的长宽比,但将其装入4/3容器中.
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
这就是我想要的:.如您所见,图像被放入"一个4/3容器中.
This is what I'd like: . As you can see, the image is "put into" a 4/3 container.
推荐答案
我的错误.诸如ImageMagick -extent中的4:3之类的宽高比只会裁切而不会填充.
My error. The aspect ratios such as 4:3 in ImageMagick -extent will only crop and not pad.
在 http://www.fmwconcepts.com/上查看我的bash Unix脚本"aspectpad" imagemagick/index.html ,它可以满足您的要求.
See my bash unix script "aspectpad" at http://www.fmwconcepts.com/imagemagick/index.html, which does what you want I think.
尽管如此,这是部分解决方案.但这仅适用于横向模式输入.由于对-extent使用了内联参数,因此也仅适用于ImageMagick 7.您必须将其修改为纵向模式.
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.
输入(纵横比2:1 = 2/1 = 2):
Input (aspect 2:1 = 2/1 = 2):
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
输出(纵横比4:3 = 4/3 = 1.33):
Output (aspect 4:3 = 4/3 = 1.33):
请注意,我使用了黑色背景,因此在这里可见.更改为您想要的任何其他颜色.
Note, that I used background of black so that it was visible here. Change to any other color you want.
如果输入横向宽高比大于4:3(4/3),它将在顶部/底部填充.如果输入横向宽高比小于4:3,它将在左/右上滑动.
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.
输入(长宽比= 1:1 = 1/1 = 1):
Input (aspect=1:1 = 1/1 = 1):
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
这篇关于如何在imagemagick中调整图像大小但保持宽高比不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!