# matplotlib-generated images meet textures

in BGE, I am trying to generate some images with matplotlib, then want to put these images as an object texture.

My first try was ok, I

• generate the image (plplot)
• save it to disk (PIL savefig)
• load the image and put it as texture (Texture imageFFmpeg)

Code:

# plot data
plt.plot(x,y)
plt.savefig('plot.png')

# put image on object as texture
matID=bge.texture.materialID(here.objects['graph1'],"MAplot1mat")
bge.logic.gp1=bge.texture.Texture(here.objects['graph1'],matID)
url_ima=bge.logic.expandPath('//plot.png')
bge.logic.gp1.source=bge.logic.ImageFFmpeg(url_ima)
bge.logic.gp1.refresh(True)


I would like to avoid saving image on disk. So I tried to use an Image Buffer to let the matplotlib image (in memory) to be saved as an image buffer, to feed into the bge.texture. No way.

Code:

bge.logic.plot=bge.texture.Texture(scene.objects['floor'],0)
image_buffer=bge.texture.imageToArray(floor_figure)
bge.logic.floorplot.source=bge.texture.ImageBuff()

• This answer might help, note some performance issues. Other answers containing image.pixels may give more examples. Oct 3 '15 at 9:17