简体   繁体   English

可以防止 ImageMagick 覆盖现有图像吗?

[英]Can ImageMagick be prevented from overwriting an existing image?

When convert ing an image, ImageMagick's default behavior seems to be to overwrite any existing file. convert图像时,ImageMagick 的默认行为似乎是覆盖任何现有文件。 Is it possible to prevent this?有没有可能防止这种情况? I'm looking for something similar to Wget's --no-clobber download option .我正在寻找类似于 Wget 的--no-clobber 下载选项的东西 I've gone through ImageMagick's list of command-line options , and the closest option I could find was -update , but this can only detect if an input file is changed.我已经浏览了ImageMagick 的命令行选项列表,我能找到的最接近的选项是-update ,但这只能检测输入文件是否已更改。

Here's an example of what I'd like to accomplish: On the first run, convert input.jpg output.png produces an output.png that does not already exist, and then on the second run, convert input.jpg output.png detects that output.png already exists and does not overwrite it.下面是想什么,我要完成一个例子:在第一次运行, convert input.jpg output.png产生output.png存在,然后在第二次运行, convert input.jpg output.png检测该output.png已经存在并且不会覆盖它。

Just test if it exists first, assuming bash :假设bash ,首先测试它是否存在:

[ ! -f output.png ] && convert input.png output.png

Or slightly less intuitively, but shorter:或者稍微不那么直观,但更短:

[ -f output.png ] || convert input.png output.png

Does something like this solve your problem?这样的事情能解决你的问题吗?

It will write to output.png but if the file already exists a new file will be created with a random 5 character suffix (eg. output-CKYnY.png then output-hSYZC.png , etc.).它将写入output.png但如果文件已经存在,则会创建一个带有随机 5 个字符后缀的新文件(例如output-CKYnY.png然后output-hSYZC.png等)。

convert input.jpg -resize 50% $(if test -f output.png; then echo "output-$(head -c5 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c5).png"; else echo "output.png"; fi)

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

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