[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

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

News123

3/3/2010 12:38:00 AM

Hi,

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?


########## Python code snippet ################

im = Image.open('grayscalefile_onebyteperpixel')
wx,wy = im.size

# create a string in order to pass it to my function
# I'm afraid this involves copying so want to get rid of it
im_as_string = im.tostring()

# create a byte array in order to store my result
new_img_array = array.array('B', [0]*(wx*wy) )


# my function which should just change some pixels has
# to copy all unmodified pixels
my_extension.my_func(wx,wy,im_as_string,new_img_array)


im = Image.frombuffer('L',(wx,wy),new_img_array)
show(im2)

############ C wrapper code snippet ###############

int wx,wy;
Py_buffer img;
Py_buffer new_img;
Py_buffer table;
unsigned char *img_ptr;
unsigned char *new_img_ptr;

int ok = PyArg_ParseTuple(args,
"iis*w*",&wx,&wy,&img,&new_img);
img_ptr = (unsigned char *) img.buf;
new_img_ptr = (unsigned char *) new_img.buf;
my_func(wx,wy,img_ptr,new_img_ptr);


Thanks in advance for any suggestions to make this more efficient.


bye


N

6 Answers

Tim Roberts

3/3/2010 6:34:00 AM

0

News123 <news123@free.fr> wrote:
>
>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?

If it were me, I'd be tempted to go peek at the source code for PIL, then
pass the Image object to my C routine and poke at the innards to find the
buffer with the pixels.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

News123

3/3/2010 8:43:00 AM

0

Hi Tim,

Tim Roberts wrote:
> News123 <news123@free.fr> wrote:
>> 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?
>
> If it were me, I'd be tempted to go peek at the source code for PIL, then
> pass the Image object to my C routine and poke at the innards to find the
> buffer with the pixels.

Yes, this might be an option
Somehow though it didn't feel right for me to depend on internal non
documented data types, which might change between releases of PIL.

N

Stefan Behnel

3/3/2010 9:10:00 AM

0

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

News123

3/3/2010 9:38:00 AM

0

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


N

Stefan Behnel

3/3/2010 10:10:00 AM

0

News123, 03.03.2010 10:37:
> Stefan Behnel wrote:
>> 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.
>
> 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.

Absolutely.


> 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.

Check out the tutorial on the web site, and ask on the cython-users mailing
list about integration with PIL. I'm sure you'll get some useful examples.

Here's something closely related, although it doesn't really use PIL (but
the mechanisms are the same):

http://wiki.cython.org/examples/...


> 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

The nice thing about Cython is that you always have a normal CPython
interpreter running, so you can split your code between the interpreter and
the compiler at any level of granularity. However, the advice was really to
write your image processing algorithm in Cython, not your entire program.

Stefan

Tim Roberts

3/5/2010 3:45:00 AM

0

News123 <news123@free.fr> wrote:
>
>Yes, this might be an option
>Somehow though it didn't feel right for me to depend on internal non
>documented data types, which might change between releases of PIL.

That's absolutely true. If performance is a priority, somethimes you have
to do things that are "dirty". Just include lots of comments saying what
you did and why.

Personally, because so much of PIL is in C, I would judge internal changes
to be unlikely.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.