creating sprite from PIL image

493 views
Skip to first unread message

Larry Keber

unread,
Mar 16, 2008, 1:30:31 PM3/16/08
to pyglet-users
Hi,
I attended the talk on pyglet here at PyCon yesterday and was very
impressed. I've been playing with it since.

I'd like to build sprite images (using PIL, although if there is a
better way I'd be glad to use it). Then, I'd like to instantiate
pyglet Sprites using the generated image data. I'm having trouble
getting it to work:

## using PIL 1.1.6 and pyglet 1.1alpha1
import pyglet
from pyglet.sprite import Sprite
from pyglet.image import ImageData
import Image, ImageDraw

pim = Image.new('RGB', (50, 50))
draw = ImageDraw.Draw(pim)
draw.ellipse((0, 0, 49, 49))
draw.text((0, 10), "sprite1")
pim.save('sprite1.jpg')

## I've tried both of the following variations
bdata = list(pim.getdata())
#bdata =
pim.getdata()

## I've tried this:
#img = pyglet.image.create(50,
50)
#img.set_data('RGB', 50,
bdata)

## and this:
img = ImageData(50, 50, 'RGB', bdata)

p = Sprite(img)
############# end of code

I always receive the following error. Am I doing something wrong and/
or stupid? Thanks for any help. -- Larry

lak@Argent:~/python/testpyglet $ python test.py
Traceback (most recent call last):
File "test.py", line 20, in <module>
p = Sprite(img)
File "build/bdist.linux-x86_64/egg/pyglet/sprite.py", line 210, in
__init__
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/PIL/__init__.py", line 780, in get_texture

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/PIL/__init__.py", line 774, in create_texture

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/PIL/__init__.py", line 948, in blit_to_texture

ctypes.ArgumentError: argument 9: <type 'exceptions.TypeError'>: wrong
type
Exception exceptions.AttributeError: "'Sprite' object has no attribute
'_vertex_list'" in <bound method Sprite.__del__ of
<pyglet.sprite.Sprite object at 0x15aef490>> ignored
lak@Argent:~/python/testpyglet $

Alex Holkner

unread,
Mar 16, 2008, 8:21:50 PM3/16/08
to pyglet...@googlegroups.com

ImageData only accepts Python strings or ctypes byte arrays for the
initial data. This works:

bdata = pim.tostring()

The image will be upside-down unless you tell ImageData that the pitch
is negative:

img = ImageData(50, 50, 'RGB', bdata, pitch=-50*3)

> Exception exceptions.AttributeError: "'Sprite' object has no attribute
> '_vertex_list'" in <bound method Sprite.__del__ of
> <pyglet.sprite.Sprite object at 0x15aef490>> ignored

This was a bug in the Sprite destructor, now fixed in r1911.

The filenames in your traceback are completely wrong (showing PIL
module instead of pyglet). This is a(nother) bug in setuptools -- I
don't recommend using eggs for development.

Alex.

Reply all
Reply to author
Forward
0 new messages