简体   繁体   English

在graphicsmagick中,如何在大量文件上指定输出文件?

[英]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? 如何使用mogrify来确定最终文件格式? For example, firstpic.png -> firstpic-thumbnail.png, secondpic.png -> secondpic-thumbnail.png, etc. 例如,firstpic.png - > firstpic-thumbnail.png,secondpic.png - > secondpic-thumbnail.png等。

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

Is there any way to do this? 有没有办法做到这一点?

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: 我不知道是否有办法从mogrify指定输出文件格式,但我会使用简单的bash循环convert

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: 或者如果你真的想使用mogrify你可以使用-output-directory这里有更多关于此选项 )将新文件放入单独的目录中,然后负责重命名:

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 ;) 我更喜欢第一种方法;)

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. 我不知道“mogrify”中的设施重命名输出文件,而不是指定不同的目录或不同的扩展名。 So fragphace's answer is correct; 因此,fragphace的答案是正确的; 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM