python 2.6: how to modify a PIL image from C without copying forth and back

News123 news123 at
Wed Mar 3 10:37:43 CET 2010

Hi Stefan,

Stefan Behnel wrote:
> News123, 03.03.2010 01:38:
>> I created a grayscale image with PIL.
>> Now I would like to write a C function, which reads a;most all pixels
>> and will modify a few of them.
>> My current approach is:
>> - transform the image to a string()
>> - create a byte array huge enough to contain the resulting image
>> - call my c_function, which copies over the entire image in order
>>     to modify a few pixels
>> How can I achieve this with the least amount of copies?
> Take a look at Cython instead, it will allow you to access PIL's image
> buffer directly, instead of copying the data. It will also simplify and
> speed up your C wrapper code.
> Stefan

I don't know Cython. Having looked at the web site I'm not entirely
sure, I understood your suggestion.

Do you mean
-  to stay with Python 2.6 and to implement only my extension in Cython.
This might be very attractive. If yes, can you recommend some url's /
tutorials / etc. which might help to implement a cython extension for
python2.6. which reads and modifies a pixel of a PIL image.

or do you mean
- to switch entirely from Python 2.6 to Cython. I would be reluctant to
do so, as I have already a lot of existing code. and I do not have the
time to check the entire code base for portability issues

thanks in advance


More information about the Python-list mailing list