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)
In the bash shell (used in Linux and other unixes, even cygwin on Windows):
ReplyDeletefor f in *.png; do convert "$f" -quality 100 "${f%%.png}.jpg"; done
I tried the reverse conversion from jpg to png
ReplyDeleteand 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.
for %f in (*.png) do (convert %f -quality 100 %~nf.jpg)
DeleteYou 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)
Thank You Pat!! Same circumstances, with hundreds of png's to convert. Done in a flash.
ReplyDeleteYou can make this portable by putting the command in a .bat file, but you have to double up on the %s to this:
ReplyDeletefor %%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!
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?
ReplyDeleteUse "Lupas Rename" to change all spaces into dots or underlines. Best regards from Poland :)
Delete