OnScreenImage from PNMImage or numpy


I’ve only been using Panda3D for a few months, but it’s great. It really helps me to display my data.

I would like to display an image with OnScreenImage. My data is already stored in a numpy array. I tried with a PNMImage but I have to save it in a file to be able to give it to OnScreenImage which is not satisfactory.

I create the image like this
myEmptyImage = PNMImage(256, 256)

and try to use it like this
self.image = OnscreenImage(image=myEmptyImage,pos=(-0.5, 0, 0.02), parent=aspect2d)

but it does not work. Error about an empty image.

I have to save it and reload it like this:
self.image = OnscreenImage(image=“test.png”,pos=(-0.5, 0, 0.02), parent=aspect2d, scale=(0.2,0.2, 0.2))

Passing my data from numpy to PNMImage is not a problem.
My problem is to pass it to OnScreenImage without having to save it into a file.


I’m not sure that OnScreenImage is intended to be used in quite this way–I note in the API that it expects either a NodePath or a string as the value of the “image” keyword-parameter.

However, you could try omitting the “image” keyword-parameter, and then subsequently calling the “setImage” method when your image is available; the API isn’t clear about what it expects to receive in the parameter to that method.

Otherwise, you could perhaps create a quad (such as via the “CardMaker” class), create and apply a Texture to that, and then load the image directly from your PNMImage into the Texture. (Which, note, doesn’t require saving the image to your file-system, I believe.)

Note that this may call for scaling the quad to fit the aspect-ratio of the image, if the image is non-square.

You can pass a Texture object to OnscreenImage. You can initialize a Texture from numpy using tex.setRamImage (after setting it up using setup2dTexture), or from a PNMImage using tex.load.

1 Like