2
$\begingroup$

Is there a way to pass a PIL/(Pillow) Image to blender game engine video texture / texture.ImageBuff() or and https://www.blender.org/api/blender_python_api_2_76_0/bge.texture.html function?

I am making something like photoshop in the bge.

$\endgroup$
2
$\begingroup$

You can it like this:

def set_image(obj, image):
    '''Sets the image used as a background. Expects an instance of PIL.Image
    CAUTION: THIS OPERATION IS SLOW'''
    if image is None:
        # Sets the image to all black
        buff = bgl.Buffer(bgl.GL_BYTE, 3)  # Blank, really small image
        width, height = [1, 1]
    else:
        width, height = image.size
        imdata = numpy.asarray(image.convert('RGB')).ravel()  # SLOW
        buff = bgl.Buffer(bgl.GL_BYTE, width*height*3, imdata)

    image_buffer = bge.texture.ImageBuff(1, 1)  # Very small image
    image_buffer.load(buff, width, height)  # Overwritten with PIL one

    if 'TEX' not in obj:
        obj['TEX'] = bge.texture.Texture(obj, 0) # 0 is the texture ID
    tex = obj['TEX']
    tex.source = image_buffer
    tex.refresh(False)
$\endgroup$
0
$\begingroup$

Moguri says you can use PYopenGL and the Post render callback to draw on a face using a PIL image. I will edit this comment and provide more details as I find them.

$\endgroup$
  • $\begingroup$ Ok, I ended up using the new BindId code to get the texture slot, and then used pyopenGL to force the texture into the texture slot* $\endgroup$ – BluePrintRandom Apr 24 '16 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.