PIL PNG encoding performance

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

PIL PNG encoding performance

Oliver Tonnhofer-4
Hi,

I found out that the default PNG(ZIP) encoding options do not offer  
the best performance and output sizes. At least for "vector" images  
like street maps.
I changed the options of deflateInit2 in ZipEncode.c and tested the  
performance of Z_FILTERED, Z_DEFAULT_STRATEGY, Z_HUFFMAN_ONLY and  
Z_RLE and I tested different compression levels for some of the  
strategies.

I found out that Z_RLE offers the best performance while offering good  
compression, at least for my test image.

I tested with timeit.
-----
t = timeit.Timer("img.save('/dev/null', 'png')",
        "import Image; img = Image.open('test.png')")
print name, min(t.repeat(3, 10)),
-----

Here are my raw results, together with the resulting file size.
default0 is the Z_DEFAULT_STRATEGY with compression level 0, etc.

default   0.785417079926 296276
default0  0.171308040619 974861
default1  0.356842041016 323340
default3  0.441128969193 310072
default5  0.562795162201 298403
default7  0.990942001343 295061
default9  7.42386984825  289238
huffman   0.482778072357 409161
huffman1  0.450232982635 409161
filtered  0.821682929993 307535
filtered1 0.360295057297 323340
rle       0.341444015503 307691

For comparison the same tests as JPEG with different quality levels.

jpeg 0.515193939209 181315
jpeg85 0.553126096725 243813
jpeg90 0.587316989899 305299
jpeg95 0.649972915649 431085
jpeg98 0.728760004044 602096
jpeg99 0.771711111069 718724

I really would like to have more options in Image.save to choose  
different compression levels/strategies. I would like to add that to  
PIL and have some questions on how to add that.

At the moment save for PNG accepts only the `optimize` option which  
enables the best/slowest compression level (9). I think it would be  
bad for backwards compatibility to use this option to set the actual  
compression level. What about compress_level and compress_type for the  
options? I would add constants for the types in Image.py.

Any opinions or suggestions on that? I would do a fork on bitbucket  
then and start hacking.

Regards,
Oliver


_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: PIL PNG encoding performance

Oliver Tonnhofer-4

On 04.03.2010, at 14:31, Oliver Tonnhofer wrote:
> Any opinions or suggestions on that? I would do a fork on bitbucket  
> then and start hacking.

Ok, I just started :)
I added compress_level and compress_type as save options for png  
files. The default behaviour is identical to 1.1.7.

Anyone care to review? http://bitbucket.org/olt/pil-117/changeset/8d4661695edd/

I load the ZLIB constants (Z_RLE, etc) into the _imaging module and  
then into Image. I'm not sure if that's OK, or if I should create some  
new constant values.

I did not prefix the constants, but maybe it is time to start with it.  
BILINEAR, RLE, FLOYDSTEINBERG, ADAPTIVE, ... it's getting a bit  
confusing.

Regards,
Oliver
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: PIL PNG encoding performance

Fredrik Lundh
On Fri, Mar 5, 2010 at 11:23 AM, Oliver Tonnhofer <[hidden email]> wrote:

>
> On 04.03.2010, at 14:31, Oliver Tonnhofer wrote:
>>
>> Any opinions or suggestions on that? I would do a fork on bitbucket then
>> and start hacking.
>
> Ok, I just started :)
> I added compress_level and compress_type as save options for png files. The
> default behaviour is identical to 1.1.7.
>
> Anyone care to review?
> http://bitbucket.org/olt/pil-117/changeset/8d4661695edd/

Thanks.  I'll take a look when I find the time (and borrow the code if
that's fine with you).

> I load the ZLIB constants (Z_RLE, etc) into the _imaging module and then
> into Image. I'm not sure if that's OK, or if I should create some new
> constant values.
>
> I did not prefix the constants, but maybe it is time to start with it.
> BILINEAR, RLE, FLOYDSTEINBERG, ADAPTIVE, ... it's getting a bit confusing.

Well, they all have the "Image." prefix :)

Not sure somewhat specialized encoder-specific settings should live in
that namespace, though; might be better to put them in the codec
module itself.  I'll think of something.

</F>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: PIL PNG encoding performance

Oliver Tonnhofer-4

On 11.03.2010, at 14:49, Fredrik Lundh wrote:
> On Fri, Mar 5, 2010 at 11:23 AM, Oliver Tonnhofer <[hidden email]>  
> wrote:
>> Anyone care to review?
>> http://bitbucket.org/olt/pil-117/changeset/8d4661695edd/
>
> Thanks.  I'll take a look when I find the time (and borrow the code if
> that's fine with you).

Sure. Do whatever you want, these changesets are CC0.

Regards,
Oliver
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig