Optimising PNG Generation in Python

From: andrew cooke <andrew@...>

Date: Sat, 16 Jul 2011 01:38:21 -0400

I'm generating images in Python using pypng http://code.google.com/p/pypng/

It's a great library (and pure Python, so it can be used in AppEngine), but I
needed the code to run as fast as possible.  Anyway, after some profiling, I
can say that for my images (which are small, stylised graphics with a
restricted number of colours), using a palette is a big win - it reduces time
spent creating (in my code) and writing (in pypng) the image.  It also reduces
the amount of memory used (which means I can cache more!).

And as far as I can tell, the "compression" parameter to Write should be 9
(the maximumum for zlib).  This does have an effect on size, and even for my
simple images it was significant and progressive.  But it doesn't have much
effect on speed, so turning to max (9) makes sense.

After a couple of hours of work, both total running time and image size are
about half what they were (I did some other code optimisatiosn too -
particularly keeping the image in HSV format until the last moment, rather
than swapping multiple times between RGB and HSV :o)

Andrew

Bit Depth?

From: andrew cooke <andrew@...>

Date: Sat, 16 Jul 2011 01:40:19 -0400

PS Curiously, changing bit depth seems to have no effect at all for paletted
images.  Too tire to work out why now, though....