PIL in panda

Does anyone have any idea how to convert a PIL image object to something Panda can use as a texture?

Is there a PIL to PNMImage function? I didn’t see one in the API.

You could use pil.save() to save it to a Python FileIO object, then extract that into a Python string, then into a StringStream object, and then read it into a PNMImage using PNMImage.read().

Or, you could use pil.tostring() to get the raw rgb data as a string, then use PNMImage.read(data, “.img”) to read it using the “raw” form. Reading raw data requires packing the width and height of the image at the head of the data stream, so this might be a little clumsier. Not obviously faster than the above approach, either.


Thanks for the reply.

What I’m really trying to do is grab a camera feed frame by frame from an OpenCV camera capture. I thought PIL would be the easiest way but I’m guessing that’s not true.

For the application we have OpenCV is doing some processing of the camera stream before we use the frames so I don’t think the OpenCVTexture() function would work for this.

Could I do something like this?:

# 'image' is the processed OpenCV frame
pmni = PNMImage.read(cv.GetSize(image), image.tostring(),".img")

Use Texture and setRamImageAs to convert a string stream to a texture object.

Thanks, I think that could work but I’m having trouble getting it right.

data = image.tostring()#CV image
self.videoTexture = Texture('movie')
self.videoTexture.setRamImageAs(data, ".img")

gives me this error:

AssertionError: image.size() == (size_t)(_component_width * format.size() * imgs
ize) at line 950 of c:\buildslave\dev_sdk_win32\build\panda3d\panda\src\gobj\tex

I’ve tried to concatenate the img size into the data by adding the img size string at the beginning of the data but this doesn’t work either. What am I doing wrong?

“.img” is not the right format. Read the docstring for setRamImageAs, it should be something like “RGBA” or so.
Furthermore, before calling setRamImageAs, you need to call setup2dTexture with the right parameters for the image format.