bashimagemagickmogrify

How do I rename images when using mogrify with Image Magick?


I am trying to automate the converting images to specified tif formats. I have it converting just fine, but am also needing to add "_GS" at the end of the file name and before the extension to each file converted. Below is what I have but have had no luck finding a solution to add "_GS" to the file name. Thanks in advance for any help.

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

Solution

  • Item 1: With Image Magick, to create a new file instead of overwriting an existing one, use convert, not mogrify (magick convert with newer versions of IM).

    Item 2: You can use shell parameter expansion to remove the extension, and then build the new filename from that and the new suffix:

    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} returns the expansion of variable with the shortest match of pattern removed from the end.