Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In graphicsmagick, how can I specify the output file on a bulk of files?

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?

like image 224
Matt Kim Avatar asked Jul 04 '14 15:07

Matt Kim


2 Answers

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 ;)

like image 159
pwlmaciejewski Avatar answered Sep 20 '22 02:09

pwlmaciejewski


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
like image 23
Glenn Randers-Pehrson Avatar answered Sep 22 '22 02:09

Glenn Randers-Pehrson