[Image-SIG] Quantization of PNG images
fredrik at pythonware.com
Mon May 18 13:10:45 CEST 2009
On Mon, May 18, 2009 at 10:15 AM, Jesper Larsen
<jesper.webmail at gmail.com> wrote:
> I am producing some png images for a web application. Some of the
> images are cached and reused for other requests. I am using matplotlib
> to produce the images. Unfortunately the images are quite large (up to
> ~300 kb). I therefore tried using the Linux utilities pngcrush and
> pngnq for compressing the images. pngnq gave by far the best results
> (~300 kb -> ~90 kb in 0.4-0.5 seconds on my laptop; combining the two
> tools did not yield further improvements). It reduces the color depth
> to 8 bits (from 24) without any apparent image quality problems using
> color quantization (http://en.wikipedia.org/wiki/Color_quantization).
> Is it possible to do this in PIL directly or will I have to code it
> myself? At present I am considering just calling pngnq from my Python
> code but I would rather not do that.
> Another thing is that for performance reasons I might want later to
> reuse the colormap (since the image colors are pretty much the same).
> Neither pngnq nor pngquant (which pngnq is based on) supports this
If you convert the image to mode "P" before you save it, you get a
fixed 8-bit palette by default (based on "web safe colors"). To have
PIL pick an "optimal" palette instead, use convert("P",
for more options.
The algorithm used by pngnq seems to be a bit better (though a lot
slower) than PIL's median cut algorithm, so you might not get quite as
good results as you get with pngnq.
More information about the Image-SIG