[cairo] Reducing PNG Image Size

Carl Worth cworth at cworth.org
Wed Sep 24 12:31:14 PDT 2008


On Wed, 2008-09-24 at 10:02 +0800, Mohit Sindhwani wrote:
> Hi!  I'm using Cairo to generate PNG files that are created by joining 
> smaller tiles together and drawing some more items on top of that.  Now, 
> in some cases, this ends up producing files that are 3000 pixels by 2000 
> pixels - which is large, but the resultant saved file is about 20MBytes.

PNG is a lossless format. So a 3000x2000x4 bytes/pixel image is 24MB of
data. As to how much zlib is able to compress that---that depends a lot
on the specific data you've got. Here you're getting things compressed
to 83%.

> Now, I read that PNG file sizes can be similar to GIF

I'm not really sure what that means. Do you want to quantize your image
down to only 256 colors like GIF is limited to?
 
> of other things on the Cairo documentation site, but I fear that I 
> haven't been able to find how to reduce/ control the size of the images 
> produced.  Any idea how I should go about it?

There are certainly no knobs exported in cairo for "controlling size".
If you do want full control,. you can ignore cairo_surface_write_to_png,
and just put your data into an image surface and do whatever games you
want to write out the data.

> I found that pngcrush can reduce the size and it claims to use libpng 
> and zlib - both of which are also being used by Cairo, so I'm wondering 
> if there's something inherent?

I don't know what pngcrush might be doing, but certainly if you're
willing to throw away some information you can generate a much smaller
file size. See what happens with a lossy format like JPEG, for example.

> By the way, in the case of these images, transparency is not needed.
> 
> A nudge in the correct direction would be appreciated!

If you find what pngcrush is doing to be acceptable, (and you can find
out exactly what that is), then you might just do something similar with
the image surface data yourself before saving the results.

Good luck,

-Carl




More information about the cairo mailing list