Shrink PNG file size from the console

How to decrease png file size dramatically

ape in a dress dancingPngquant bills itself as a command-line utility and a library for lossy compression of PNG images. Over on Sourceforge, optipng claims to be a PNG optimizer that recompresses image files to a smaller size, without losing any information. Combining optipng’s optimization with pngquant’s compression can drastically decrease png file sizes.

WordPress, not happy with just being a blogging software, has grown into an extremely popular do-it-all framework. Let’s use some of the basic icons and logo images provided with every installation to showoff the power of optipng and pngquant. The wp-admin/images directory contains a bunch of images, utility stuff.

Let’s look at two files, the largest in the directory and another somewhere in the middle that already seems pretty small to begin with.

Major Reduction

icons32-vs-2x.png is 37,994 bytes. Squash it with pngquant’s lossy image compression, then re-optimize it with optipng and it’s down to 21,803 bytes. That’s almost half the original size. Yet to the naked eye, the data loss is, for most intents and purposes, unnoticeable.

Minor Reduction Army

Let’s examine the w-logo-blue.png for instance. This is used as the stylized ‘W’ icon that shows up on the login page.
Provided by WordPress, it’s a whopping 3,112 bytes. After lossy image compression and re-optimization, it’s only 3,093 bytes. A waste of time for a minor reduction of 1%, but not when you repeat this process for hundreds of images. The real power is obvious when you shrink a few larger files but every bit counts, literally. Multiply that squad of itty-bitty images by the number of times your pages are served and now we’re getting somewhere.

So let’s crush a whole directory at once.

Compress

Run pngquant on all the files ending in ‘png’ and do it in place instead of creating new files by specifying the extension with ‘-ext’.

for x in `ls *.png`; do pngquant -f -ext .png $x; done

Optimize

Squeeze out a little more by optimizing the new files with optipng.

for x in `ls *.png`; do optipng -o7 $x; done

You can use the most exhaustive search “not generally recommended” by optipng’s help page, probably because it takes so long and doesn’t make much difference. May as well if you don’t care how long it takes.

for x in `ls *.png`; do optipng -zc1-9 -zm1-9 -zs0-3 -f0-5 $x; done

Leave a Reply

Your email address will not be published. Required fields are marked *