ImageMagick调整为抽象矩形 [英] ImageMagick resizing to an abstract rectangle

查看:72
本文介绍了ImageMagick调整为抽象矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的图像尺寸未知.我想缩小它,使其适合 640x480或480x640,并保持宽高比.我希望图像缩小最小数量(例如,结果应为适合640x480或480x640的最大尺寸).

I'm given an image of unknown size. I want to shrink it such that it will fit in either 640x480 or 480x640, preserving aspect ratio. I want the image to shrink the minimum amount (e.g. the result should be the maximum size which fits in either 640x480 or 480x640).

例如,如果我有800x800的图像,则应缩小为480x480.如果我有500x1000的图片,则应缩小到320x640.同样,1000x500应该变成640x320.

For example, if I have a 800x800 image, it should shrink to 480x480. If I have a 500x1000 image, it should shrink to 320x640. Similarly, 1000x500 should become 640x320.

我可以使用一个命令在ImageMagick中执行此操作吗?请勿裁切,并且应保留原始图像的纵横比.谢谢!

Can I do this in ImageMagick in one command? No cropping should occur, and the aspect ratio of the original image should be preserved. Thanks!

推荐答案

很容易在不裁剪和保留长宽比的情况下调整图像大小,但是我认为您无法实现一个命令.

It's easy to resize an image without cropping and preserving the aspect ratio, but I don't think that you'll be able to achieve your either/or in a single command.

几何规范文档中,调整超大图像的大小到640x480很容易:

From the geometry specification docs, resizing an overlarge image to 640x480 is easy:

convert input.png -resize 640x480> output.png

那只会在必要时调整大小,不会裁剪,并且会保留长宽比.

That will only resize if necessary, won't crop, and will preserve the aspect ratio.

根据您输入的图像,您也许可以使用区域运算符:

Depending on your input images, you might be able to use the area operator:

convert input.png -resize $((640*480))@ output.png

但这仅在所有输入图像已经具有4/3或3/4的长宽比的情况下起作用.

But that will only work if all the input images already have either a 4/3 or 3/4 aspect ratio.

我认为您最好的选择是使用简短的脚本:

I think your best bet is a short shell script:

wider_than_tall=`identify -format %w/%h input.png`
if (( $wider_than_tall )); then
  convert input.png -resize 640x480> output.png
else
  convert input.png -resize 480x640> output.png
fi

这篇关于ImageMagick调整为抽象矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆