Reducing png file size

Jesper Larsen <jesper.webmail@...287...> writes:

Unfortunately the files are quite big (up to ~300 kb). I have however
tried using the Linux tool pngnq to reduce the file size with a factor
~3-4 with almost no degradation of the result.

  Pixel depth (Pixel Depth): 32
  Colour Type (Photometric Interpretation): RGB with alpha channel

  Pixel depth (Pixel Depth): 8
  Colour Type (Photometric Interpretation): PALETTED COLOUR (256
colours, 0 transparent)

This means pngnq has quantized the original RGBA image with 8 bits per
channel to an image with a 256-color palette. I don't think Agg has any
support for rendering directly to a paletted image, so to achieve
similar results, you would have to do the quantization in a separate
pass anyway.

I am not using transparency for anything. For a web application a
reduction from 300 kb to 90 kb is really important so I hope you have
some good ideas.

A web application needs to be fast, right? According to its home page,
pngnq "is limited mostly to off-line uses rather than real time image
delivery". You could take a look at PIL to see if it has any fast
quantization algorithms, and pass your result to it as in the
to_numeric.py example (see also webapp_demo.py for how to avoid using
the pylab machinery for figure management). If not, you could always
implement some fast quantization algorithm in numpy:

http://en.wikipedia.org/wiki/Color_quantization

My guess is that if you always produce similar-looking images, you could
fix the palette off-line using whatever fancy algorithm you like, and
then the actual conversion could be done pretty fast, especially if you
can forgo dithering - perhaps for many types of charts it is not
necessary.

···

--
Jouni K. Sepp�nen
http://www.iki.fi/jks