473,383 Members | 1,829 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,383 software developers and data experts.

Transparent PNG and PIL

I am trying to create a simple script that Opens an existing PNG and
resaves it with a transparent color. With the command line version of
ImageMagic this is an easy endevor (and my current solution). I can
simply do something like:

convert -transparent black image.png image-transparent.png

(That makes anything black transparent in the saved image.)

It would seem that using PIL would make this simmilarly simple, but so
far I have not found a simple clear explanation of how to do this. If
someone has figured this out could you post it for the benefit of
mankind. ;)

On a somewat off topic matter (as far as Python goes), I see that
transparent PNGs, as created by tools such as image magic, do not
display transparent on MS Internet Explorer. I know that this is
probably because of MS's draggin their feet on supporting standards.
But, I noticed that with tools such as PaintShop Pro I can add
transparency to a PNG, and have it show correctly in IE. Does it do
this by in effect creating a proprietary PNG format?

When adding transparency to the PNG file, PaintShop says that the
image needs to be reduced to a single, paletted, background layer. It
then decreases the color depth to 256 colors. And then allows one to
set a color as transparent. When saving the image it warns that no
alpha channel will be saved. Nevertheless, the transparency is saved
correctly and displays correctly on all the tools I've tried. Can
someone shed a little light on what is going on here?
Jul 18 '05 #1
4 11617
R.Marquez wrote:
It would seem that using PIL would make this simmilarly simple, but so
far I have not found a simple clear explanation of how to do this. If
someone has figured this out could you post it for the benefit of
mankind. ;)
if you have an 8-bit palette image (mode "P"), you can specify the
transparency color (as a color index) when you save the image:

im.save(filename, transparency=0)

for RGB images, you can use the putalpha methods to add a transparency
layer to the image (turning into a mode "RGBA" image).
Does it do this by in effect creating a proprietary PNG format?


iirc, IE supports transparency masks for 8-bit palette images (mode "P"),
but it does not support transparency with true color images ("RGBA").

unless you use IE-specific filters, that is:

http://webfx.eae.net/dhtml/pngbehavior/pngbehavior.html

</F>


Jul 18 '05 #2
"Fredrik Lundh" wrote:
if you have an 8-bit palette image (mode "P"), you can specify the
transparency color (as a color index) when you save the image:

im.save(filename, transparency=0)


Not quite so simple. I tried this:
image = Image.open("Some.png") #This file has transparency set correctly. image.mode 'P' image.info {'transparency': 0} image.save("SomeT.png", trasparency=0)

When I open the new image with Paintshop it seems to think that the
image has trasparency set, but when I request to see it, nothing is
shown. What is more, when I open the image with PIL I see this:
image2 = Image.open("Test.png")
image2.mode 'P' image2.info

{}

So it seems to have lost the transparency.

The PIL handbook doesn't mention transparency as an option of the save
method for PNG files. It only mentions that the open method sets the
transparency info property for transparent palette images.
Jul 18 '05 #3
"R.Marquez" wrote:
image.info {'transparency': 0} image.save("SomeT.png", trasparency=0)


"transparency", not "trasparency".

(the PNG writer ignores unknown configuration options)

</F>


Jul 18 '05 #4
"Fredrik Lundh" wrote:
"transparency", not "trasparency".


Oh. I forgot that Python is not only case sensitive but spell
sensitive as well. :o) Thank you.

-Ruben
Jul 18 '05 #5

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

Similar topics

1
by: Efkas | last post by:
My application have some level : 1. MyButton class with Label inheritance 2. MyComponent as User Control loading and positionning some of MyButtons 3. MyApp loading and positionning MyComponent ...
3
by: Steve Koon | last post by:
Any thoughts on getting this project to work or suggesting another method would be appreciated. Steve ========================================== Project: A Windows Form which acts as a...
7
by: Peter Oliphant | last post by:
Using MakeTransparent one can supposedly turn a color used in a Bitmap to transparent. But, it looks to me like all it does it set these pixels to the color BackColor of the Control it's attached...
5
by: Mark Deibert | last post by:
I'm a former VB6 coder. Quit a few years ago. Now I'm back and trying to teach myself VB.NET. I don't remember having this much difficulty learning VB6. I'm totally stuck on something and need your...
8
by: Grahammer | last post by:
Is it possible to set the background of a usercontrol as transparent? I tried setting the background image of the usercontrol to a transparent GIF, but that caused MAJOR problems. I'm making...
4
by: jcrouse | last post by:
I am using the following code to move a label on a form at runtime: If myMousedown = lblP1JoyRight.Name Then If lblP1JoyRight.BackColor.Equals(Color.Transparent) Then bTransCk = True ...
2
by: Pascal | last post by:
Je veux que mes label soit transparent aussi sur mes picturebox alors j'écris : i want my labels to be transparent on my pictureboxes so i wrote : Private Sub Form1_Load(ByVal sender As Object,...
4
by: ray well | last post by:
in my app i need to make a RichTextbox control transparent. i need it to be a like a pane of glass lying on a sheet of paper, where u can see everything on the sheet of paper not covered by text...
4
by: Dale | last post by:
I am creating GIF images with transparent backgrounds on-the-fly for a web app and rendering them by using System.Drawing.Image.Save(Response.OutputStream, ImageType.GIF). I am confident that...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.