473,439 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,439 software developers and data experts.

Overlaying transparent images with PIL

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

---------------
Jul 18 '05 #1
2 13723
Quoting Fearless Freep (jo******@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

Jul 18 '05 #2
Geoff Gerrietts <ge***@gerrietts.net> wrote in message news:<ma**********************************@python. org>...
Quoting Fearless Freep (jo******@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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: matt | last post by:
Hi, I have two pairs of image. In each pair there is one image that is masked by the other. I have then attempted to overlay them. However, the image that is being masked has white corners. How...
0
by: Rene | last post by:
Hi, I have a form with an image with transparanent area´s. When the form loads the form seems to be transparent, but when I move the form (catched mousdown and mousemove events) the background...
1
by: Tim Geiges | last post by:
I wrote an image viewer and just added the zoom feature, so when the image is larger than the form scrollbars appear, but when I scroll the image(if it has transparency) it leaves a trail of image...
0
by: felix | last post by:
Hi, I'm trying to subclass System.Windows.Forms.Control to create a Control that is fully transparent. I'd like to place this control over other Controls in order to display additional...
3
by: MW de Jager | last post by:
Im trying to place an image on my form, which is transparent in parts like an icon. My page refuses to display the transparent areas in the color of the page background but keeps replacing it with...
3
by: Christopher Kurtis Koeber | last post by:
Dear All, I know that I am asking a lot about images lately, but it is just that new issues keep arising. My issue now has to do with transparent images. When I display transparent images,...
0
by: nets-rac | last post by:
Hi, I want to add an image to a powerpoint presentation with c#. That works fine if the image has no transparent background. If I use an image with a transparent background the pixels in the...
2
by: moishyyehuda | last post by:
Does any one know how to make a transparent image with specifically PIL, but any standard python library will do. I need a spacer, and it has to be transparent. Thanks
1
by: Tea Maker | last post by:
Hi, I know that GIF and PNG can be transparent, but from my experience in creating XHTML/CSS files, I always used to avoid using transparency because in Internet Explorer 6 the transparency...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.