Hello,
I ended up using PIL; in this case, it is the simplest method to use.
Because I am doing the operation at scene-load-time, I am not really
concerned with performance. There are probably some small
optimizations I could do, namely to avoid having to load so much from
file. I am displaying the code here, even though it's not directly
pyglet-related, to help out anyone who's trying to do the same thing:
from PIL import Image
# .... this belongs in some class definition
def build(_):
"""
format = [
('img_file', (posx, posy), <rotation>, <scale>),
]
"""
# empty.png should be a completely transparent image
full_img = Image.open('data/parts/
empty.png').resize((150,150))
try:
for part in _.parts:
part_pil = Image.open('data/parts/'+part[0])
try:
part_pil = part_pil.rotate(part[2], Image.BICUBIC,
True)
scalesize = tuple([xy * part[3] for xy in
part_pil.size])
part_pil = part_pil.resize(scalesize,
Image.BICUBIC)
except IndexError:
pass
# position transformation last!
# scale might change image size
pil_pos = (
part[1][0] - part_pil.size[0]/2 + full_img.size[0]/
2,
part[1][1]*-1 - part_pil.size[1]/2 +
full_img.size[1]/2,
)
# using the image itself as a mask, so we don't
overwrite
# non-alpha pixels in underlying images with alpha
pixels
full_img.paste(part_pil, pil_pos, part_pil)
# lazy man's image conversion...
filename = 'cache/temp_completed_ship.png'
full_img.save(filename)
ship_img = pyglet.image.load(filename)
_.sprite = pyglet.sprite.Sprite(ship_img)
except AttributeError:
print "WARNING: couldn't build %s; no parts" % (repr(_))
Thanks,
Kenny
On Oct 16, 4:44 pm, "Alex Holkner" <[EMAIL PROTECTED]> wrote:
> On 10/15/08, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Hello,
>
> > I am wondering what I should do if I want to construct an image
> > programatically from a set of other images. What I'm trying to do is
> > create an image of a spaceship, given a series of images for each part
> > (like an engine, cockpit, fuselage) that can be translated, rotated,
> > scaled, etc. such that you could make a large number of different-
> > looking ships with code.
>
> > I think I am on the right track, creating a sprite for each part,
> > performing the various transformations, and then extracting the image
> > data with "sprite.image.get_image_data()". For now, I am also loading
> > a blank image with full transparency on every pixel and scaling it to
> > the size that I need, in order to fit the part images. However, I
> > cannot successfully use 'blit_into' like so:
>
> > full_img.blit_into(part_sprite.image.get_image_data(), x, y, 0)
>
> > I end up getting this error:
>
> > File "C:\devel\pyglet\image\__init__.py", line 1598, in
> > blit_into
> > source.blit_to_texture(self.target, self.level, x, y, z)
> > File "C:\devel\pyglet\image\__init__.py", line 948, in b
> > lit_to_texture
> > data)
> > File "C:\devel\pyglet\gl\lib.py", line 105, in errcheck
> > raise GLException(msg)
> > pyglet.gl.lib.GLException: invalid value
>
> > I honestly don't know what's going on there, even after looking at the
> > code in question. I'm betting someone has done something like this
> > before, what am I doing wrong?
>
> Make sure that the source image fits completely within the bounds of
> the destination image at the blit location you've requested. Note
> that the anchor_x and anchor_y properties affect the blit location (if
> you don't know what these are, they don't apply to you).
>
> Failing that, I suggest posting a complete code example, including the
> images used, so that other members of the list can reproduce the
> problem and offer suggestions.
>
> Alex.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---