[Image-SIG] Optimizing PNGs in PIL

Mikael Olofsson mikael at isy.liu.se
Thu Nov 4 14:19:10 CET 2004


I've been playing around with PIL for a while, and I am quite pleased so 
far, but I have a small problem. I can save an image using


as the PIL Handbook at


says, and it also states that I can give options by supplying a named 
argument called options. Now to the problem. I have an RGB image that I want 
to save as a PNG, and I want the file to be as small as possible. For my 
particular image: If I do not supply any options, I get a file of size 
125kB. If I understand the Handbook correctly, I should be able to 
"instructs the PNG writer to make the output file as small as possible" by 
supplying an option called optimize. I've interpreted that as

    im.save('c:/foo/bar/baz.png', options='optimize')

The resulting file is still 125kB. So, the option does not seem to make any 
difference, at least not for my image.

To check if that is a reasonably small file size, I tried to open the file 
in Gimp and in XnView and simply save a PNG copy of the image using Save As. 
In both cases the new files were smaller, namely 81kB and 89kB respectively, 
i.e. approximately 2/3 of the original file size. For my particular 
application a file size of 80 kB is acceptable, even though my hope has been 
to reach 50kB.

Am I using options correctly? Is this what should be expected?

FYI: I do have the ZLIB compression library installed. However, I did not 
install everything myself, so I am not sure if  ZLIB was installed before 
PIL or after. I guess that it was installed in the correct order, since PIL 
allows me to produce PNGs. I base that guess on "To enable PNG support, you 
need to build and install the ZLIB compression library before building the 
Python Imaging Library" from the Handbook, but perhaps that only refers to 
the optimize option.

/Mikael Olofsson
Universitetslektor (Associate professor)
Linköpings universitet

E-Mail:  mikael at isy.liu.se
WWW:     http://www.dtr.isy.liu.se/en/staff/mikael
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Linköpings kammarkör: www.kammarkoren.com   Vi söker tenorer och basar! 

More information about the Image-SIG mailing list