PIL GIF transparency


D

David

Hello alll,

Apologies if this isn't the correct list.

I'm getting my feet wet with the Python Imaging Library (PIL) 1.1.6 on
Python 2.5.1. I have played around quite a bit with transparent GIFs,
and from what I can tell, PIL should support what I want to do -
except I can't seem to get it to work.

I have a large number of smallish icons (GIFs with transparency) that
I'd like to tile into a grid in one large GIF image. The background
color of the image should be GIF-transparent. Currently, I can set it
to be white or black or any other color I want, just not transparent.
My current code:

import os, sys
from PIL import Image, ImageColor

# size in pixels of each row/col
ROWSIZE = 50
COLSIZE = 50

# maxwidth of sprite grid
SPRITEWIDTH = 20
MAXWIDTH = COLSIZE*SPRITEWIDTH

FILENAME = '/static/images/icons.gif'

def merge():
pwd = os.getcwd()
imagePath = os.path.join(pwd, 'images')

# Somehow, I need to ensure the palette used in the sprite image
# is the same as the individual icons
sprites = Image.new('RGBA', (MAXWIDTH, MAXWIDTH), (253,253,253,0))
sprites.putalpha(0)

palette = None
colors = []
col = 10
row = 10

for f in os.listdir(imagePath):
try:
im = Image.open(os.path.join(imagePath, f))

if im.size[0] <= 20 and im.size[1] <= 20 and im.format ==
'GIF':
sprites.paste(im.copy(), (col, row))
print 'url(/static/images/%s) => url(%s) -%dpx -%dpx'
% (f, FILENAME, col, row)
col += COLSIZE
if col >= MAXWIDTH:
row += ROWSIZE
col = 10

if row >= sprites.size[1]:
break

except IOError, ex:
pass

sprites = sprites.crop((0,0,MAXWIDTH, row)).point(lambda pt: pt ==
(253, 253, 253, 0) and 0 or pt)
return sprites

So this code is obviously littered with the detritus of failed
attempts to get transparency to work. Has anyone out there had any
experience with this?

Thanks,

David
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top