[Image-SIG] uchar pointer to image

Douglas Bagnall douglas at paradise.net.nz
Thu Jun 2 04:25:10 CEST 2005


W. John wrote:
> Hi!
>   This is a newbie question about PIL and Python. If I
> have a shared character array (buffer) that is
> allocated in a C++ class using "mmap".  A function
> called 'Capture' returns a pointer to that shared
> area, and after a wrapper for Python I'm doing
> something like this:
>    >data=mycam.Capture()
>    >print data
>    _0810cdbe_p_uchar
> 
> How can I convert "data" (the uchar pointer) to an PIL
> image?  Do I need to use the "fromstring" fuction?  

Image.fromstring expects a python string with the image bytes in it.
In C you would do something like this:

     return Py_BuildValue("s#", &the_uchar_image_array, 640*480);

returning a long string that would print as apparent nonsense, but would
be understood by Image.fromstring.

>>> image = Image.fromstring("L", (640, 480), data)

There is also a Image.frombuffer, and a python buffer object, which will
let you get away with less copying, but I've never used those.

http://python.org/doc/2.4.1/api/bufferObjects.html
http://effbot.org/imagingbook/image.htm

I've also never used C++, so maybe none of this applies.

douglas



More information about the Image-SIG mailing list