471,330 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

PIL GIF transparency

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

Sep 25 '07 #1
1 4308
En Mon, 24 Sep 2007 21:53:46 -0300, David <da******@gmail.comescribi�:
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.
Read this post from 2006:
<http://groups.google.com/group/comp.lang.python/msg/9c33aea70b72d78b>

--
Gabriel Genellina

Sep 25 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Cecile Muller | last post: by
57 posts views Thread by Piotr Wolski | last post: by
4 posts views Thread by Matthias Czapla | last post: by
2 posts views Thread by Robin Becker | last post: by
3 posts views Thread by Mr. x | last post: by
20 posts views Thread by Nathan Sokalski | last post: by
6 posts views Thread by tommaso.gastaldi | last post: by
reply views Thread by ZikO | last post: by
3 posts views Thread by Martijn Mulder | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.