[Image-SIG] I'm trying to use the Python Imaging Library to draw
transparent GIFs onto a TKinter canvas.
ericford at comcast.net
Mon Jun 7 14:07:24 EDT 2004
I'm trying to use the Python Imaging Library to draw transparent GIFs onto
a TKinter canvas.
Following is some sample code I found, but I'm wondering if there's
anything better out there.
The image is first split into red green blue and alpha (opacity) channels,
then a function is applied to each of the RGB layers which results in a
Here is the code fragment that accomplishes this:
source = im.split() # split the image into layers
R, G, B, A = 0, 1, 2, 3 # for readability
mask = im.point(lambda i: i < 255) # kluge
source[A].paste(mask) # put mask into the alpha channel
im = Image.merge(im.mode, source) # build a new multiband image
self.graphic = ImageTk.PhotoImage(image=im) # for eventual retrieval
The kluge here is that the point method applies its function to each
layer, so instead of inputting a 24 bit color as the transparent color, we
only get to specify an 8 bit value. Any pixel having a value of 255 in
one of the layers will be treated as transparent. This happens to work
fine with the current color choices in the GIF files, but a real solution
needs to be found.
More information about the Image-SIG