简体   繁体   English

ImageMagick命令以相同名称进行转换和保存

[英]ImageMagick command to convert and save with same name

I am using ImageMagick convert command for making thumbnails & save the converted images in another directory, one by one, in PHP. 我正在使用ImageMagick convert命令制作缩略图,并将转换后的图像一个接一个地保存在PHP中。

But, cant figure out how to keep the the image name in the converted image. 但是,无法弄清楚如何在转换后的图像中保留图像名称。

> convert 1.JPG -resize 120X120 thumb/*.JPG

need to keep the output file names same as the input. 需要保持输出文件名与输入相同。 Please help. 请帮忙。

Another way: 其它的办法:

convert *.jpg -resize 80% -set filename:f '%t' ../'%[filename:f].jpg'

Will place converted files in the folder above. 将转换后的文件放在上面的文件夹中。

The option -set filename:f '%t' sets the property filename:f to the current filename without the extension. 选项-set filename:f '%t'将属性filename:f设置为当前文件名,不带扩展名。 Properties beginning with filename: are a special case that can be referenced in the output filename. filename:开头的属性是一种特殊情况,可以在输出filename中引用。 Here we set it to ../'%[filename:f].jpg , which ends up being the image filename with the extension replaced with .jpg in the parent directory. 在这里,我们将其设置为../'%[filename:f].jpg ,最终它是图像文件名,其扩展名在父目录中被替换为.jpg

Documentation references: 文档参考:

A simple solution would be copy, followed by mogrify - another imagemagick tool - this will keep the same names, it takes all the same args as convert. 一个简单的解决方案是复制,然后是mogrify(另一个imagemagick工具),它将保持相同的名称,它的所有参数与convert相同。

cp *.jpg thumb/
cd thumb
mogrify -resize 120X120 *.JPG

Alternatively you could do a bit of shell scripting, using find -exec or xargs 或者,您可以使用find -exec或xargs进行一些shell脚本编写

# using -exec
find . -iname "*.JPG" -maxdepth 1 -exec convert -resize 120x120 {} thumbs/{} \;

# using xargs
find . -iname "*.JPG" -maxdepth 1 | xargs -i{} convert -resize 120x120 {} thumbs/{}

Another easy way that doesn't involve a lot of typing is GNU Parallel : 另一个不涉及大量输入的简单方法是GNU Parallel

parallel convert {} -resize 120X120 thumb/{} ::: *.jpg

convert is called for each of the files given after ::: , and {} is replaced with the file name for each invokation. :::之后,为每个给定的文件调用convert ,并且{}被每次调用的文件名替换。 This will also process the files in parallel, so it's likely a lot faster than the other solutions here. 这也将并行处理文件,因此它可能比此处的其他解决方案快很多。

It also works if you want to convert the file type: 如果要转换文件类型,它也可以使用:

parallel convert {} {.}.png ::: *.jpg

{.} is replaced with the filename without extension, allowing you to change it easily. {.}替换为不带扩展名的文件名,使您可以轻松更改它。

Here's what I do: 这是我的工作:

Convert all files to filename-new.extension 将所有文件转换为filename-new.extension

for FILE in *; do convert -resize 320 $FILE $(echo $FILE | sed 's/\./-new./'); done

Move all filename-new.extension files back to filename.extension : 将所有filename-new.extension文件移回filename.extension

for FILE in *; do mv $FILE $(echo $FILE | sed 's/-new//'); done

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

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