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

991 views Asked by At

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
1

There are 1 answers

6
Shawn On BEST ANSWER

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.