如果您在 Linux/OSX 上,您可以像这样获得图像尺寸:
identify -format "%w %h" input.jpg
所以,如果您想要变量w 和h 中的宽度和高度,请执行以下操作:
read w h < <(identify -format "%w %h" input.jpg)
现在您可以测试宽度和高度,并在必要时进行进一步处理:
[ $w -gt 300 -o $h -gt 100 ] && convert input.jpg -set units ...
或者,如果你想更详细:
if [ $w -gt 300 -o $h -gt 100 ]; then
convert ...
fi
所以,我提出的整体解决方案如下所示:
#!/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 没有区别,所以如果您选择的格式,只需将我的 JPG 替换为 PNG。
已针对 Windows 更新
好的,没有其他人在帮忙,所以我会拿出我(非常)生疏的 Windows 技能。在 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
您现在应该在两个变量w 和h 中获得图像input.png 的宽度和高度,通过键入检查
echo %w%
echo %h%
现在你需要做一些IF 语句:
if %w% LEQ 300 GOTO SKIP
if %h% LEQ 100 GOTO SKIP
convert ....
:SKIP
注意::您可能需要在 Windows 中的百分号前面加上^。
注意:您可能需要双 @ 登录脚本,因为 Windows 不合逻辑。