Transparency Fails on .GIF

66 views
Skip to first unread message

Paul

unread,
Aug 19, 2011, 3:08:25 PM8/19/11
to pyglet-users
import pyglet
from pyglet.gl import *

---CODE---

window = pyglet.window.Window(500, 500)

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

animation = pyglet.image.load_animation('animation.gif')
sprite = pyglet.sprite.Sprite(animation)
map = pyglet.image.load('map.png')

@window.event
def on_draw():
window.clear()
map.blit(0, 0)
sprite.set_position(sprite.x, sprite.y)
sprite.draw()
def update(dt):
sprite.x += dt * 22
sprite.y = 117
pyglet.clock.schedule_interval(update, 1/60.)
pyglet.app.run()

---CODE---
ok, so this works(gives no errors), but the gif has a white
background? even though i made it transparent in gimp.
plz help???
thank you very much for your time

Adam Bark

unread,
Aug 19, 2011, 3:36:16 PM8/19/11
to pyglet...@googlegroups.com

I think you'll find that gif doesn't support transparency
http://en.wikipedia.org/wiki/Gif maybe you should save your images as
pngs instead; as you mention you're using the gimp that should be trivial.

HTH,
Adam.

Paul

unread,
Aug 19, 2011, 3:43:53 PM8/19/11
to pyglet-users
thanks

Paul

unread,
Aug 19, 2011, 3:54:59 PM8/19/11
to pyglet-users
how would i go about making an animation out of 2 png's? use
'add_to_texture_bin()'???

Adam Bark

unread,
Aug 19, 2011, 4:00:06 PM8/19/11
to pyglet...@googlegroups.com
On 19/08/11 20:54, Paul wrote:
> how would i go about making an animation out of 2 png's? use
> 'add_to_texture_bin()'???
>

I think the usual method used is to update the sprite's image as
required. I think generally the most efficient way to do that is to make
an animation strip and use whichever bit of the strip you need.

Paul

unread,
Aug 19, 2011, 4:42:55 PM8/19/11
to pyglet...@googlegroups.com
so it would not be an animation then right/?? it would just switch sprite images at a time interval?

Michael Red

unread,
Aug 19, 2011, 4:51:49 PM8/19/11
to pyglet...@googlegroups.com
Since that's what animations generally are, yes, it would. You can also use color key-ing. Load the .gif (where you replaced the background with a color that doesn't show up anywhere else, so you know what is transparent and what not), then go through each frame and where a pixel's color is like the transparent one you chose, set alpha to 0. It's a lot of work, generally. Better to use image switching.

On 19 August 2011 23:42, Paul <thepa...@gmail.com> wrote:
so it would not be an animation then right/?? it would just switch sprite images at a time interval?

--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pyglet-users/-/Kxqo9h2IupYJ.

To post to this group, send email to pyglet...@googlegroups.com.
To unsubscribe from this group, send email to pyglet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyglet-users?hl=en.

Mike Redhorse

unread,
Aug 19, 2011, 5:29:46 PM8/19/11
to pyglet-users
http://remike.homelinux.org/pyglet/index.php?topic=9.0

Discussed a bit further in that topic. It seems .gif support has been
broken somehow for Linux. If anyone else can test, that would be
great. Loading .gif animations with Ubuntu (PIL installed, GTK2.0 up
to date, non-development pyglet) with load_animation gives me this:

> raise first_exception
> pyglet.image.codecs.ImageDecodeException: Unable to load: circlewalk.gif

This shows that all the decoders are failing for it. Switching
manually to a PIL decoder loads the image, but transparent pixels are
turned into black. Windows appears to load it just fine.


On Aug 19, 11:51 pm, Michael Red <mike.r...@gmail.com> wrote:
> Since that's what animations generally are, yes, it would. You can also use
> color key-ing. Load the .gif (where you replaced the background with a color
> that doesn't show up anywhere else, so you know what is transparent and what
> not), then go through each frame and where a pixel's color is like the
> transparent one you chose, set alpha to 0. It's a lot of work, generally.
> Better to use image switching.
>

Tristam MacDonald

unread,
Aug 22, 2011, 1:05:31 AM8/22/11
to pyglet...@googlegroups.com
On Fri, Aug 19, 2011 at 3:36 PM, Adam Bark <adam....@gmail.com> wrote:
I think you'll find that gif doesn't support transparency
http://en.wikipedia.org/wiki/Gif maybe you should save your images as
pngs instead; as you mention you're using the gimp that should be trivial.

HTH,
Adam.

GIF supports transparency just fine, albeit not *partial* transparency. Because it doesn't support partial transparency, setting the background to transparent in Photoshop/GIMP is not the correct way to control GIF transparency. You need to explicitly create an alpha channel for your image, and see the transparent sections in that.

However, I would recommend you use PNG's in general - it is a significantly more modern format, and does support partial transparency.

--
Tristam MacDonald
System Administrator, Suffolk University Math & CS Department
Reply all
Reply to author
Forward
0 new messages