如何使用 ImageMagick "convert" 工具将输出文件名打印到控制台?

How to print output filename to console using ImageMagick "convert" tool?

当我使用 ImageMagick "convert" 工具转换图像时,我想获取创建文件的文件名。

使用“-monitor”命令行参数我只能得到输入文件名。

这更像是一种解决方法...如果您使用变量提前指定输出,则可以从变量中获取输出。下面是一个使用 shell 脚本的例子:

#!/bin/bash                                                                     
export fn="fuzzy-magick"
export inp="$fn.png"
export outp="$fn.gif"
convert $inp $outp
echo "Output was "$outp

更新答案

最简单、最直接的方法是使用 -verbose 选项,如下所示:

convert rose: rose: rose: -verbose image_%04d.png
rose:=>image_0000.png[0] PPM 70x46 70x46+0+0 8-bit sRGB 6.97KB 0.000u 0:00.000
rose:=>image_0001.png[1] PPM 70x46 70x46+0+0 8-bit sRGB 6.97KB 0.000u 0:00.000
rose:=>image_0002.png[2] PPM 70x46 70x46+0+0 8-bit sRGB 6.97KB 0.000u 0:00.000

我花了好几次迭代和太多的时间才到达那里,但我会把我早期的想法留在下面,以防有人想尝试一些 "off the wall" 并且,比方说,"contrived",做类似事情的方法...

选项 1

您可以像这样使用 %p 转义序列以及 -format+identify

convert rose: rose: rose: -format "image_%p.png\n" -identify image_%04d.png
image_0.png
image_1.png
image_2.png

是的,我知道它不是很完美,但它可能足以让您入门。

选项 2

这可能是另一种选择:

convert rose: rose: rose: -verbose +identify 'rose-%04d.png' | grep png
rose:=>rose-0000.png[0] PPM 70x46 70x46+0+0 8-bit sRGB 7.06KB 0.000u 0:00.000
rose:=>rose-0001.png[1] PPM 70x46 70x46+0+0 8-bit sRGB 7.06KB 0.000u 0:00.000
rose:=>rose-0002.png[2] PPM 70x46 70x46+0+0 8-bit sRGB 7.06KB 0.000u 0:00.000

选项 3

convert -debug "Trace" rose: rose: rose: image_%04d.png 2>&1 | grep "\.png" | sort -u
image_%04d.png
image_0000.png
image_0001.png
image_0002.png

选项 4

另一种选择可能是创建一个文件来标记当前时间,然后 运行 您的命令并查找比您在开始之前创建的文件更新的文件:

touch b4; sleep 1; convert rose: rose: rose: image_%04d.png

find . -newer b4

./image_0000.png
./image_0001.png
./image_0002.png

选项 5

您建议使用 %o(输出文件名)转义的另一个选项 - 以及 -verbose

convert rose: rose: rose: -format "%o" -verbose -identify image_%04d.png
rose:=>image_0000.png[0] PPM 70x46 70x46+0+0 8-bit sRGB 6.97KB 0.000u 0:00.000
rose:=>image_0001.png[1] PPM 70x46 70x46+0+0 8-bit sRGB 6.97KB 0.000u 0:00.000
rose:=>image_0002.png[2] PPM 70x46 70x46+0+0 8-bit sRGB 6.97KB 0.000u 0:00.000

扩展 Mark Setchel ,我这样做是为了获取文件名:

FILES=( $(
  convert <arguments> \
    -format "%o\n" -verbose -identify \
    <output> \
    2>&1 > /dev/null | sed -nEe 's/.*=>(.*)\[.*//p'
) )

例如:

$ FILES=( $(
  convert rose: \
    \( -clone 0 -modulate 100,100,33.3 \) \
    \( -clone 0 -modulate 100,100,66.6 \) \
    -format "%o\n" -verbose -identify \
    rose.png \
    2>&1 > /dev/null | sed -nEe 's/.*=>(.*)\[.*//p'
) )
$ echo ${FILES[0]}
rose-0.png
$ echo ${FILES[1]}
rose-1.png
$ echo ${FILES[2]}
rose-2.png
$ echo ${#FILES[@]}
3