PNGcrush + bash = LosslessLove
Last post I used Pngcrush to compress a set of images. I thought I’d just post the scripts I use almost daily to keep my PNG assets lean.
Honestly I wish Pngcrush had these options without the need for crazy bash find or for-loops. But hey, It does one thing and does it well. I’m not complaining for its awesomeness.
All of these scripts use the –brute option which brute forces its way through numerous compression options until it finds the most effective way to describe your image in PNG for the smallest filesize.
Compress all PNG’s in a directory and replace them with compressed version
for i in *.png;do  pngcrush -brute -d tmp "$i" ; done; mv -f tmp/* .; rm -rf tmp
Compress PNG’s and put them in a new directory called ‘tmp’
find * -name "*png" -execdir pngcrush -d tmp {} ;
Recursively explore directories for PNG’s and compress then replace with compressed version
for file in `find . -name "*.png"`;do  echo $file;  pngcrush -brute "$file" tmp_img_file.png;  mv -f tmp_img_file.png $file; done;
April 4th, 2007 at 5:26 pm
For inkscape.org I wrote a little Java program for this kind of thing. It does a little book-keeping (CRCs and all that) and it also generates pretty nice statistics.
Thanks to the book-keeping you can run it over a working copy of a website over and over again without reprocessing the already crunched files again.
Instead of PNGcrush I used the /really/ slow, but slightly more effective PNGOUT:
http://advsys.net/ken/utils.htm (also available for Mac and Linux)
Source:
http://inkscape.org/crunchman/CrunchMan.java
Statistics from inkscape.org:
http://inkscape.org/crunchman/inkscape_web.cmlog.html
(As you can see many of the really big files were already well compressed… so, the overall stats aren’t that impressive.)
Example command line:
java CrunchMan D:\inkscape_web “pngout.exe /kbKGD /q *” inkscape_web.cmlog
The command line I actually used (for starting pngout at a low priority):
java CrunchMan D:\inkscape_web “cmd /q /c compress.bat *” inkscape_web.cmlog
compress.bat:
start /low /b /wait pngout.exe /kbKGD /q %1
I could have used a more excessive compress.bat file, but the first pass took already over 48 hours (spread across several days) on my puny 500mhz machine.
April 4th, 2007 at 9:18 pm
shouldn’t programs like gimp and inkscape run pngcrush as part of their export/save as png?
April 5th, 2007 at 7:50 am
Sam, well thew brute method does take a time.
This is undesired behaviour.
Also, creating another dependancy for inkscape is harly a new present to most package maintainers
April 6th, 2007 at 5:19 am
It does indeed take *lots* of time.
The compression of Inkscape’s PNGs is alright. It’s not overly impressive, but it’s a good compromise. I think it’s (Deflate) level 9, which is pretty much the same thing as the “maximum” Zip compression setting in many archiving applications. That means you get rather good compression (unlike Photoshop’s export for example) for very little time.
PNGOUT, PNGcrush etc compress stuff over and over again to find a hopefully optimal (”very good” actually) solution. Doing that for a somewhat bigger image on a slower machine can easily take hours, which usually isn’t acceptable.
PNG isn’t all that great anyways. It’s just not as crap as GIF. Something like PNG with LZMA instead of Deflate would be a lot better. Given how long it took Microsoft to support PNG properly… it’s only a matter of another decade.
April 17th, 2007 at 8:17 pm
If you’re looking for lossy png quantizing, pngnq is pretty good.
April 19th, 2007 at 8:50 pm
Ta Andy. =]