Search code examples
imagemagickoutputfilenamesimagemagick-convert

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


When i convert images using ImageMagick "convert" tool i want to get filenames of the created files.

Using "-monitor" command-line argument i can get only input filenames.


Solution

  • Updated Answer

    The simplest, and most direct, way of doing this is to use the -verbose option as follows:

    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
    

    It took several iterations and far too much dinking around for me to get there, but I'll leave my earlier ideas below in case anyone wants to try out some "off the wall" and, let's say, "contrived", ways of doing similar things...

    Option 1

    You can use the %p escape sequence and -format and +identify like this:

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

    Yes, I know it's not quite perfect, but it may be good enough to get you started.

    Option 2

    This might be another option:

    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
    

    Option 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
    

    Option 4

    Yet another option might be to create a file to mark the current time, and then run your command and find any newer files than the one you created before you started:

    touch b4; sleep 1; convert rose: rose: rose: image_%04d.png
    
    find . -newer b4
    
    ./image_0000.png
    ./image_0001.png
    ./image_0002.png
    

    Option 5

    Yet another option using the %o (output filename) escape you suggest - along with -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