Forum Archive

Can use PIL image for SpriteNode.texture?

mmontague

Hi all,

In the previous beta, I was able to set the SpriteNode.texture using the image identifier returned from load_pil_image(). E.g. when initializing the SpriteNode:

my_img = Image.open('my_image_file.png')  
my_sprite = SpriteNode(texture=load_pil_image(my_img), position=(0,0))

And when I set SpriteNode.texture directly, I found I needed to use a Texture object but using load_pil_image() stilll worked, e.g.:

my_sprite.texture = Texture(load_pil_image(my_img))

However, now (with the new beta) I find that neither of the above works. If I attempt either of the above Pythonista just closes. It seems that I need to first convert my image to a ui.Image before setting the SpriteNode.texture.

The documentation for scene.Texture does state that "Textures can be initialized using either the name of a built-in image (a string), or a ui.Image object." No mention here of supporting PIL images. However, as I said I could do this previously... and so I'm wondering if truly the SpriteNode.texture is only meant to be used with ui.Image (or built-in images), and if all PIL images will then need to be converted to ui.Image in order to be used with a SpriteNode.

Or if I'm doing something wrong.

Serpensine

I have noticed the same thing. At the moment I am converting a PIL image to a ui.Image using the following code (where img is the PIL image)

imgfile = StringIO.StringIO()
img.save(imgfile, format='PNG')
imagestring = imgfile.getvalue()
ui_image = ui.Image.from_data(imagestring)

This is the best method I have found in the beta, but unfortunately I don't think there are any built-in functions to do it for you.