PIL GIF transparency

Discussion in 'Python' started by David, Sep 25, 2007.

  1. David

    David Guest

    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
     
    David, Sep 25, 2007
    #1
    1. Advertising

  2. En Mon, 24 Sep 2007 21:53:46 -0300, David <> escribi�:

    > 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
     
    Gabriel Genellina, Sep 25, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marek

    PIL and transparency in IE

    Marek, Jun 26, 2003, in forum: Python
    Replies:
    0
    Views:
    1,062
    Marek
    Jun 26, 2003
  2. jean-Baptiste Cazier

    Transparency with PIL and paste ?

    jean-Baptiste Cazier, Jul 23, 2003, in forum: Python
    Replies:
    0
    Views:
    636
    jean-Baptiste Cazier
    Jul 23, 2003
  3. Robin Becker

    PIL & TIFF transparency?

    Robin Becker, Jul 30, 2003, in forum: Python
    Replies:
    2
    Views:
    736
    Robin Becker
    Jul 31, 2003
  4. Gustavo Campanelli

    Newbie : Gif transparency with Pil problem.

    Gustavo Campanelli, Dec 15, 2003, in forum: Python
    Replies:
    2
    Views:
    344
    Gustavo Campanelli
    Dec 16, 2003
  5. manjula
    Replies:
    1
    Views:
    1,439
    Roedy Green
    Oct 30, 2007
Loading...

Share This Page