Alexandru Circus
Alexandru Circus

Reputation: 5538

Convert multiple images with ImageMagick

I want to convert all my PNGs to PNG using ImageMagick (I need this because AndroidStudio has some issue with the original PNGs. ImageMagick is able to fix this issue by re-exporting the PNG.)

If I do: convert a.png a.png it works.

But how do I do this for many files (including files from sub directories)?

Upvotes: 1

Views: 1581

Answers (2)

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90213

For Windows users -- you can try this in a cmd.exe window:

for %i in (*.png *\*.png *\*\*.png *\*\*\*.png) do (
   convert.exe %i %~pni---repaired.png
)

This will loop through the current dir's PNGs as well as the ones in the sub directories 3 levels deep.

You have to make sure that your convert.exe really is the one from ImageMagick -- set up your environment variable %PATH% accordingly. Otherwise you may run into an error when the command wants to use the identically named disk format conversion convert.exe command.

If unsure, use the full path to the IM convert.exe, e.g.:

D:\programs\imagemagick-install-dir\convert.exe %i %~pni---repaired.png

Also, remember: If you put the above command into a *.bat file, you have to double up each occurrence of %. So %i from the direct command becomes %%i in the batch file!


If you are on Vista/Windows7/2008/8 (or on Windows XP with the Resource Kit installed) you'll have the ForFiles.exe available, which can be used to loop through files:

forfiles.exe   ^
  /p <path>    ^
  /m *.png     ^
  /s           ^
  /C "convert.exe @file @fname---repaired.png"

Upvotes: 1

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90213

Try this command. Start it from the top-most directory from where you want to convert all images:

find . -name "*.png"     \
| while read image; do   \
     convert "${image}" "${image/.png/---repaired.png}
  done

Caveats: Should you have PNG files with the suffix .PNG or .pNg or similar, the command will not work for these. For such cases, the command needs some modifications...

Upvotes: 1

Related Questions