Overlaying transparent images with PIL

F

Fearless Freep

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
it.

Any suggestions?

Take care,
Jay O'Connor
http://www.cybermesa.com/~joconnor/r4hsoftware
- Custom Web and Application Software for Small Businesses



---------------
 
G

Geoff Gerrietts

Quoting Fearless Freep ([email protected]):
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.
 
F

Fearless Freep

Geoff Gerrietts said:
Quoting Fearless Freep ([email protected]):

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top