Overlaying transparent images with PIL

Geoff Gerrietts geoff at gerrietts.net
Fri Sep 12 00:45:48 EDT 2003


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!

--G.

-- 
Geoff Gerrietts <geoff at gerrietts dot net>      http://www.gerrietts.net/
   "If I were two-faced, would I be wearing this one?" --Abraham Lincoln





More information about the Python-list mailing list