Optimize all PNG images recursively

Windows batch file syntax sometimes is a pain compared to UNIX bash/sh. Using optipng I wanted to shrink all the PNG images in a directory and inside its subdirectories as well. Notice that I had to write the command into a .bat batch file, it did not work directly from the command line:

FOR /F "tokens=*" %%G IN ('dir /s /b *.png') DO optipng -nc -nb -o7 -full %%G

The optipng params -nc and -nb prevent any color and color depth changes of the png files. Those may change the appearance, for example in the evil Internet Explorer. -o7 means the best and slowest optimization and -full does a full scan of the IDAT part. Using UNIX the same job may look like:

find . -name *.png | xargs optipng -nc -nb -o7 -full

8 thoughts on “Optimize all PNG images recursively

  1. If you have a directory containing a space the following command should be used:
    FOR /F “tokens=*” %%G IN (‘dir /s /b *.png’) DO optipng -nc -nb -o7 -full “%%G”


  2. An alternative using find’s exec switch:

    find . -name *.png -exec optipng -nc -nb -o7 -full {} +

    The ‘+’ passes all the files to optipng at once, which may be quicker.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s