generating icons -in game- from a camera (working script)

hey hello, it’s been a while :slight_smile:

Im looking for a way to generate an image file during the game, like a 128x128 .jpg whatever the viewport size is.

it looks you can not use makescreenshot for this need, so I was thinking about videotexture, generate a 128x128 texture from the scene, then write it… ?

import bge
from bge import texture
data =  texture.ImageViewport()
# data =  texture.ImageRender()
# bytes = ... ?
path = g.expandPath('//icons/test.jpg')
file = open(path,'wb')

thanks for any pointers.

ie :
in the plane to the left, a videotexture.ImageViewport() source cropped to 128x128. I want to generate a .jpg from it.

The ImageRender class has an “image” attribute that might be of use.

all of the classes have the same image attribute, it’s a buffer type, but you cant write it directly.

You shouldn’t need to write it? What are you trying to do, do you want to load it into the engine as a texture, or just export it as an image?

If you would simply like to write the image to a file, then try using the Python Image Library (PIL), and write the image. I haven’t actually used the image attribute from VideoTexture objects. It might not return what it seems to infer in the documentation, in which case imageToArray will be the method you need to use:

VideoTexture -> Array -> PIL -> file

sorry If I was not clear (and it seems I didn’t understand your first answer:)) : I want to export a texture as an image file.
will try something about the Array -> PIL step, thanks agoose77
I just hope I will find something about the raw data format of pil.

ok, I got this working using this module :slight_smile:
as PIL it’s for python 2.x so you need to 2to3 it. you only need the converted in <blender>/python/lib (105Ko)

import VideoTexture
import png

# the source is rgba format here
data = VideoTexture.ImageViewport()

pxls_in = VideoTexture.imageToArray(data)[0:] # data.image renvoie le meme repr()
pxls_out = []
pxls_per_row, rows = data.size
row_id = 0
while row_id &lt; rows :
	row_in = pxls_in[row_id*pxls_per_row*4:row_id*pxls_per_row*4+pxls_per_row*4]
	row_id += 1
# uncomment to see input format
# path = g.expandPath('//screenshots/test.txt')
# file = open(path,'w')
# file.write(repr(pxls_out))
# file.close()

path = g.expandPath('//screenshots/test.png')
f = open(path,'wb')
w = png.Writer(pxls_per_row, rows,alpha=True)
w.write(f, pxls_out)