Overlaying transparent images with PIL

Fearless Freep joconnor at cybermesa.com
Fri Sep 12 05:03:24 CEST 2003

I know there's a PIL mailing list but I thought I would try the
question here as well.

I'm using PIL on Python 1.5.2 (stop laughing, it's what the ISP has
for CGI and I don't have a choice)

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.

I thought using GIF with transparency would do the trick but it
doesn't seem to be working.

Here's an example of what I am doing

background = Image.open ("images/background.gif")
overlay = Image.open ("images/overlay1.gif")

overlaySize = overlay.size

# a box for the overlay dimensions...offset by 100,200
overlayBox = (100, 200, 100 + overlaySize[0], 200 + overlaySize[1])

background.paste (overlay, overlayBox)
backgroud.save ("images/tempfile.gif", "GIF")

Now, overlay2.gif is a gif file with a some transparency.  However,
where the transparency is becomes white instead.

I suspect that I a mask image will be involved in the solution, but
I'm not sure how to make a mask for my overlay image, or how to use

Any suggestions?

Take care,
Jay O'Connor
 - Custom Web and Application Software for Small Businesses


More information about the Python-list mailing list