Overlaying transparent images with PIL

Fearless Freep joconnor at cybermesa.com
Fri Sep 12 12:00:40 EDT 2003


Geoff Gerrietts <geoff at gerrietts.net> wrote in message news:<mailman.1063342041.22810.python-list at python.org>...
> Quoting Fearless Freep (joconnor at cybermesa.com):
> > 
> > Anyway.  I have a basic image and I need to overlay an image on top of
> > it and let some of the basic image show through.  Just as an aside the
> > images are not of the same size and the top image needs to be pasted
> > over the underlying image at an offset from the origin.
> 
> What you want is to convert both your images (or at least the top
> one?) to an RGBA image. The RGB are the color codes, and the "A" is
> the alpha layer. The alpha layer specifies how opaque each pixel
> should be, 0 being transparent and 255 being opaque.
> 
> If you convert the GIF you're using into an RGBA image, this should
> work. The problem with GIF is that it's not really 'transparent', the
> pixels are actually colored. There's just a special marker in the GIF
> file format that allows you to identify one of the colors as
> "transparent".
> 
> I believe the PNG file format actually provides a full four-layer
> model with variable transparency, but I'm a long way from expert or
> even knowledgable in these affairs. I just know what's worked for me
> before....
> 
> Best of luck!


What I found out would work is I could create 'transparency mask' of
the original image and then fill in the transparent part of the image
with a background color that I'm not using in the rest of the image.

---------------
colorTable = [255]*256
colorTable[0] = 0 #anything black (0) will be made transparent

mask = overlay.point (colorTable, '1') #make the transparency mask

#paste the overlay into the base image in the boundingBox using mask
as a filter
baseimage.paste (overlay, boundingBox, mask)
-----------------

Everything with a zero value (black) in the original will be
transparent in the final

Thanks


Take care,
Jay O'Connor
http://www.cybermesa.com/~joconnor/r4hsoftware
 - Custom Web And Application Software for small business




More information about the Python-list mailing list