5

I want to convert a group of files, but not overwrite the existing file. How can I use mogrify to specificy the final file format? For example, firstpic.png -> firstpic-thumbnail.png, secondpic.png -> secondpic-thumbnail.png, etc.

gm mogrify -size 150x150 *.png -resize 150x150 +profile "*" "%f-thumbnail.png"

Is there any way to do this?

Barry Kelly
  • 41,404
  • 5
  • 117
  • 189
Matt Kim
  • 759
  • 7
  • 19

3 Answers3

5

I don't know if there's a way to specify output file format from mogrify but I would use convert with simple bash loop instead:

for f in *.jpg; 
do 
    convert "$f" -resize 150x150 +profile "*" "${f%.jpg}-thumbnail.jpg"; 
done;

Or if you really want to use mogrify you can use -output-directory (more on this option here) to put new files into separate directory and then take care of renaming:

mkdir tmp;
gm mogrify -output-directory tmp -size 150x150  -resize 150x150 +profile "" "*.jpg";
for f in tmp/*.jpg; 
do 
    mv "$f" "${f%.jpg}-thumbnail.jpg"; 
done;
mv tmp/* .;
rm -rf tmp;

I like the first method better ;)

  • This answer is correct (+1). For one-off runs I use a script to run "gm convert" for each, but if I'm doing a lot of images I generally use something like your second method, to avoid having to reload "gm" repeatedly. It seems to me that "-name filename" or similar would be a useful option to add to mogrify. Both ImageMagick and GraphicsMagick have a "-name" option but the documentation of it is pretty sparse and it is not recognized by "mogrify". – Glenn Randers-Pehrson Jul 05 '14 at 17:40
2

If the output format (extension) is different from the input format, the files won't get overwritten. If they are the same, you can use this trick, that makes them appear to be "different" for this purpose but are really the same but differ in the case of the extension:

gm mogrify -resize 150x150 -format PNG +profile "*" *.png

EDIT:

I don't know of a facility within "mogrify" to rename the output files other than specifying a different directory or a different extension. So fragphace's answer is correct; you will need to use a script to rename them. In combination with my answer:

gm mogrify -resize 150x150 -format PNG +profile "*" *.png
for file in *.PNG
do
   basename=`echo $file | sed -e "s/.PNG//"`
   mv $basename.PNG $basename-thumbnail.png
done
Glenn Randers-Pehrson
  • 11,940
  • 3
  • 37
  • 61
  • I want to keep the file extension the same but append the filename..so the original file would look like photo.png and the new file would look like photo-thumbnail.png. I want to add "thumbnail" to every file in the directory. Is there a way to do that? – Matt Kim Jul 05 '14 at 14:01
  • Not directly. Fragphace is correct; you have to use a script. I've modified my answer accordingly. – Glenn Randers-Pehrson Jul 05 '14 at 17:22
0

There is an even simpler way using xargs:

ls *.jpg | xargs -I{} sh -c 'gm convert -resize 150x150 {} +profile "*" ./$(basename {} .png)-converted.jpg'
GBBL
  • 576
  • 1
  • 6
  • 18