图像缩小脚本在 linux 中创建更大尺寸的图像

Image downsizing script creates image of bigger size in linux

我有以下名为 wallpaperise.sh 的脚本,它拍摄图像并将其转换为相同的图像,但这次只有 16:9 分辨率。

## Usage: ./wallpaperise.sh <imagename.jpg>

aw=16   #desired aspect ratio width...
ah=9    #and height

in=""
out="wallpaperised_"

wid=`convert "$in" -format "%[w]" info:`
hei=`convert "$in" -format "%[h]" info:`

tarar=`echo $aw/$ah | bc -l`
imgar=`convert "$in" -format "%[fx:w/h]" info:`

if (( $(bc <<< "$tarar > $imgar") ))
then
  nhei=`echo $wid/$tarar | bc`
  convert "$in" -gravity center -crop ${wid}x${nhei}+0+0 "$out"
elif (( $(bc <<< "$tarar < $imgar") ))
then
  nwid=`echo $hei*$tarar | bc`
  convert "$in" -gravity center -crop ${nwid}x${hei}+0+0 "$out"
else
  cp "$in" "$out"
fi

问题是它创建的图像(即使它根本不改变图像)的文件大小比原始文件大吗?

我该怎么做才能解决这个问题?

使用 ImageMagick 的转换工具转换 JPEG 时,您可以选择自己的文件大小,从 -quality 1(小)到 -quality 100(大)。

顾名思义,文件大小与图像质量直接相关。

如果您不指定,ImageMagick's documentation 表示它将尝试猜测输入的质量级别,如果不能,则默认为 92。在您的情况下,它保守地选择了更高质量的设置并导致了更大的文件。

你可以试试-quality 80,看看它的样子,并根据需要增加或减少。