How to add suffix to mogrify - imagemagick

3.6k views Asked by At

I'm running this command to create thumbnails with mogrify and it works great!

#! /bin/bash
 mogrify             \
-resize 300x300     \
-crop 200x200+0-20 \
-gravity center   \
-format jpg       \
-quality 100       \
-path thumbs      \
 *.jpg

But what I would like to add is a suffix to the output filenames like. -avatar So the output image name is changed from testimage.jpg to testimage-avatar.jpg.

Thanks all!

1

There are 1 answers

5
Mark Setchell On BEST ANSWER

I don't think you can do it with mogrify, but you can use convert:

convert result.png -set filename:new '%t-avatar' %[filename:new].jpg

and you would have to put it in a loop over all JPEG files:

shopt -s nullglob
for f in *.jpg; do
   convert "$f" -set filename:new "%t-avatar" "%[filename:new].jpg"
done

Alternatively, you could retain your original mogrify command - which is actually more efficient than convert and then go into the thumbs directory afterwards and use rename to put the avatar bit in:

rename --dry-run -X --append="-avatar" *.jpg

Sample Output

'a.jpg' would be renamed to 'a-avatar.jpg'
'image.jpg' would be renamed to 'image-avatar.jpg'