473,748 Members | 6,370 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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...of fset by 100,200
overlayBox = (100, 200, 100 + overlaySize[0], 200 + overlaySize[1])

background.past e (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 13760
Quoting Fearless Freep (jo******@cyber mesa.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
"transparen t".

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***@gerriett s.net> wrote in message news:<ma******* *************** ************@py thon.org>...
Quoting Fearless Freep (jo******@cyber mesa.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
"transparen t".

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
1954
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 can I make these corners transparent. An example is shown at http://portcanal.co.uk/filter.html What I would like is for the background to the yellow triangle pair to be transparent. How do I do this? Thanks.
0
1162
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 is moved too. The same happens when moving other forms over the transparant form, the transparent area's are messed up. What is the guideline to use transparent images? Use the image as background or use some other method? Should I use the form...
1
1711
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 ghosts from the previous position. does anyone know how to stop this from happening?
0
1178
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 information. Unfortunately, as soon as the Control is placed on to with BringToTop(), all underlying Controls aren't painted anymore. The constructor looks like this:
3
1408
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 white, causing the image to sit in a white square. Is there something I'm missing here like a property that needs to be set? I've tried Icons, PNG file with transparency and Bitmap with transparency, none seem to work.
3
1885
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, sometimes either the previous image displayed is now in the transparent area or sometimes even the buffer that is on the screen. I tried to refresh the control but still no luck. Any suggestions? Thank you in advance! Sincerely, Christopher Koeber
0
1266
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 image are very square edged especially if the image contains text. is there a possibility to solve this problem? By the way...I use following method to add the picture:
2
1812
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
1524
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 doesn't work and the image will be filled in white where the transparency occurs (where in IE7, Firefox and Opera transparency works). Anyway, I fixed this issue before by making the image underneath the transparency, a part of the main image, I guess...
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4606
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.