ImageMagick改变图像的宽度和高度 [英] ImageMagick change image width and height

查看:237
本文介绍了ImageMagick改变图像的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ImageMagick通过使用以下命令行选项调整图像分辨率

I am using ImageMagick to resize image resolution by using below command-line option

转换abc.png -set units PixelsPerInch -density 75 abc_a.png

我需要这个:如果任何图像的宽度超过300 OR 100高度,我想将其转换为宽度300宽度和100高度,更改高于dpi(即75dpi)。

I am in need of this: if any images has more than 300 width OR more than 100 height, I want to convert it to width 300 width and 100 height, with changing above dpi (i.e. 75dpi).

任何人都可以帮助我吗?

Can any one help me on this?

推荐答案

如果您使用的是Linux / OSX,则可以获得如下图像尺寸:

If you are on Linux/OSX, you can get the image dimensions like this:

identify -format "%w %h" input.jpg

因此,如果您想要变量 w h 中的宽度和高度,请执行以下操作:

So, if you want the width and height in variables w and h, do this:

read w h < <(identify -format "%w %h" input.jpg)

现在你可以测试了宽度和高度,并在必要时进行进一步处理:

Now you can test the width and height and do further processing if necessary:

[ $w -gt 300 -o $h -gt 100 ] && convert input.jpg -set units ...

或者,如果你想要更详细:

Or, if you want to be more verbose:

if [ $w -gt 300 -o $h -gt 100 ]; then
    convert ...
fi

所以,我的整体解决方案建议如下:

So, the total solution I am proposing looks like this:

#!/usr/bin/bash
read w h < <(identify -format "%w %h" input.jpg)
[ $w -gt 300 -o $h -gt 100 ] && convert input.jpg -set units ...

JPEG PNG 没有区别,所以只需用 PNG替换我的 JPG 如果这是您选择的格式。

JPEG or PNG makes no difference, so just replace my JPG with PNG if that is the format of your choice.

针对Windows更新

好的,没有其他人在帮忙,所以我会解决我的(非常)生锈的Windows技能。在Windows下获取这样的图像宽度:

Ok, no-one else is helping so I will get out my (very) rusty Windows skills. Get the image width something like this under Windows:

identify -format "%w" input.png > w.txt
set /p w=<w.txt

现在得到高度:

identify -format "%h" input.png > h.txt
set /p h=<h.txt

你现在应该拥有图像的宽度和高度 input.png 包含2个变量, w h ,请输入

You should now have the width and height of image input.png in 2 variables, w and h, check by typing

echo %w%
echo %h%

现在你需要做一些 IF 语句:

Now you need to do some IF statements:

if %w% LEQ 300 GOTO SKIP
if %h% LEQ 100 GOTO SKIP
convert ....
:SKIP

注意::您可能需要 ^ 在Windows中的百分号前面。

Note:: You may need ^ in front of the percent sign in Windows.

注意:您可能需要加倍 @ 在脚本中签名,因为Windows不合逻辑。

Note: You may need double @ signs in scripts because Windows is illogical.

这篇关于ImageMagick改变图像的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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