ImageMagick Batch Convert Files

I recently shot a wedding for a friend and found myself with a few hundred images in RAW format, of which I processed the handful that I liked. Due to Flickr increasing the maximum file sizes you can upload to 50MB, I went ahead and saved my final results as lossless PNG images.

However, I also wanted to upload the images to a photo printing service that apparently does not accept PNGs as a valid photo file. (stupid AdoramaPix...).

So the question became, how can I batch convert all of the PNG images into JPG images on the windows command line?

I already had ImageMagick installed (and highly recommend it if you ever need to do batch image processing on your machine). So the question now becomes, how can I use IMs convert command to do this?

The answer is right here:

for %f in (*.png) do (convert %f -quality 100 %~nf.jpg)

What a royal PITA it was tracking this down. In a nutshell:

for %f in (*.png)

For each variable (f in my case) in all the files that end in .png in my current directory...


do (convert %f -quality 100

convert the file with a quality of 100...


%~nf.jpg)

and name the output the original file name plus .jpg as an extension. That last bit was a real pain to get right, and required me looking into the Microsoft documentation of FOR.

So, if you happen to be on windows, and have Imagemagick installed - here is the solution to batch converting images in a directory.

Oh, and you can expand the types of images and lump them all together at once. For instance, instead of *.png, you could have told it to do .png and .tif images:

for %f in (*.png *.tif) ...

Hopefully this snippet will be useful to some fellow traveler... (relevant xkcd)


7 comments:

  1. In the bash shell (used in Linux and other unixes, even cygwin on Windows):

    for f in *.png; do convert "$f" -quality 100 "${f%%.png}.jpg"; done

    ReplyDelete
  2. I tried the reverse conversion from jpg to png
    and got this error
    The following usage of the path operator in batch-param
    substitution is invalid: %~nf.png)


    For valid formats type CALL /? or FOR /?
    "convert - Copy.bat" was unexpected at this time.

    ReplyDelete
    Replies
    1. for %f in (*.png) do (convert %f -quality 100 %~nf.jpg)

      You tried this command, but reversed the jpg/png, so you called this command _exactly_ in your command window?

      for %f in (*.jpg) do (convert %f -quality 100 %~nf.png)

      I've tried it here, and it works fine. You're not calling this from a batch file, are you? (the Copy.bat statement has me a little confused)

      Delete
  3. Thank You Pat!! Same circumstances, with hundreds of png's to convert. Done in a flash.

    ReplyDelete
  4. You can make this portable by putting the command in a .bat file, but you have to double up on the %s to this:

    for %%f in (*.png) do (convert %%f -quality 100 %%~nf.pdf)

    Just put the .bat file in a folder full of .png files and double-click it to run, no command window required. Note I'm converting .png to .pdf, but .jpg works too. Very useful, thanks!

    ReplyDelete
  5. hey it works lika a charm to a file with no space in the filename, how to make it work too on the file with a space?

    ReplyDelete
    Replies
    1. Use "Lupas Rename" to change all spaces into dots or underlines. Best regards from Poland :)

      Delete