使用带有 Image Magick 的 mogrify 时如何重命名图像?

问题描述

我正在尝试将图像自动转换为指定的 tif 格式。我让它转换得很好,但我还需要在文件名的末尾和每个转换文件的扩展名之前添加“_GS”。以下是我所拥有的,但没有找到将“_GS”添加文件名的解决方案。在此先感谢您的帮助。

for f in "$@"
do
    echo "$f"
/usr/local/bin/mogrify -density 300 -resize 1000x1000 -type grayscale -define tiff:endian=msb -compress LZW -format tif "$f" [0] 
done

解决方法

第 1 项:使用 Image Magick,要创建新文件而不是覆盖现有文件,请使用 convert,而不是 mogrifymagick convert 用于较新版本的 IM)。>

第 2 项:您可以使用 shell 参数扩展来删除扩展名,然后从中构建新文件名和新后缀:

for f in "$@"
do
    echo "$f"
    /usr/local/bin/convert "$f" -density 300 -resize 1000x1000 -type grayscale \
      -define tiff:endian=msb -compress LZW -format tif "${f%.*}_GS.tiff"
done

${variable%pattern} 返回 variable 的扩展,并将 pattern 的最短匹配从末尾移除。