Bash:如果 width/height 超过特定值,则批量调整图像大小

Bash: batch resize images if width/height exceeds a specific value

如果输入图像宽度或高度超过特定值(Linux 或 Mac OS X,命令行),是否有任何方法可以批量调整图像大小?

我在这里找到了一张 similar question,但那个只能用于一张图片。

可能的解决方案:

#!/bin/sh

set -e
maxwidth="1900"  # in pixels, the widest image you want to allow.

#find all .jpg in current dir and subdirectories
FILES="$(find . -iname '*.jpg')"

for imagefile in $FILES
do
if [ -f "$imagefile" ]; then
imgwidth=`sips --getProperty pixelWidth "$imagefile" | awk '/pixelWidth/ {print }'`
else
    echo "Oops, "$imagefile" does not exist." 
    exit
fi

if [ $imgwidth -gt $maxwidth ]; then
    echo " - Image too big. Resizing..."
    sips --resampleWidth $maxwidth "$imagefile" > /dev/null 2>&1  # to hide sips' ugly output 
    imgwidth=`sips --getProperty pixelWidth "$imagefile" | awk '/pixelWidth/ {print }'`
    imgheight=`sips --getProperty pixelHeight "$imagefile" | awk '/pixelHeight/ {print }'`
    echo " - Resized "$imagefile" to $imgwidth""px wide by $imgheight""px tall";
fi
done

使用来自 ImageMagick 套件的 mogrify 可能:

mogrify -resize 1024x768\> *.jpg

按比例缩小所有超过 1024x768 的 jpeg。首先在您的图像的副本上尝试。添加 -path output 以将结果写入名为 output 的子目录 - 首先使用 mkdir output.