tags:

views:

250

answers:

2

I am having an issue with an embedded 64bit Python instance not liking PIL. Before i start exhausting more methods to get a compiled image editor to read the pixels for me (such as ImageMagick) i am hoping perhaps anyone here can think of a purely Python solution that will be comparable in speeds to the compiled counterparts.

Now i am aware that the compiled friends will always be much faster, but i am hoping that because i "just" want to read the alpha of a group of pixels, that perhaps a fast enough pure Python solution can be conjured up. Anyone have any bright ideas?

Though, i have tried PyPNG and that is far too slow, so i'm not expecting any magic solutions. None the less, i had to ask.

Thanks to any replies!

And just for reference, the images i'll be reading will be on average around 512*512 to 2048*2048, and i'll be reading anywhere from one to all of the pixels alpha (multiplied by a few million times, but the values can be stored so reading twice isn't done).

+2  A: 

Getting data out of a PNG requires unpacking data and decompressing it. These are likely going to be too slow in Python for your application. One possibility is to start with PyPNG and get rid of anything in it that you don't need. For example, it is probably storing all of the data it reads from the PNG, and some of the slow speed you see may be due to the memory allocations.

Ned Batchelder
Yea, thats about what i figured. I had to take a stab in the dark and ask it anyway :)Thanks for the comment!
Lee Olayvar
A: 

When you say PyPNG is too slow, how slow is it? To put it another way, how fast would be fast enough? PyPNG doesn't do anything stupid to make itself slow, but it is written in Python.

Make sure you're using read() to read the image row by row, and make sure you're using row[3::4] to extract the alpha channel. Extracting the alpha channel by using slice notation is no slower than reading the whole image.

I've added some notes to the PyPNG documentation about its speed.

David Jones
In particular: decompression uses the zlib module, which is in C and fast; PyPNG doesn't store the whole image in memory unless it has too, it generally processes the image as a row by row stream.
David Jones
It's been a while since i've used it but think of it this way, i eventually figured out how to get PIL working with the embedded situation and the end result was that PIL, even with it's custom compiled modules, still is a bit slow. Not that it's slow itself, for what it does, etc. Simply slow in the context of what the end user sees, and not what's "realistically" possible. So, in the context of "*It would be great if even PIL was 5x faster*", PyPNG can't compete.Again, i don't expect PyPNG to compare in that context, i'm just explaining what "fast enough" is here. :)
Lee Olayvar