Transparent PNG and PIL

R

R.Marquez

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?
 
F

Fredrik Lundh

R.Marquez said:
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>
 
R

R.Marquez

Fredrik Lundh said:
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:

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:
{}

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

Fredrik Lundh

"transparency", not "trasparency".

(the PNG writer ignores unknown configuration options)

</F>
 
R

R.Marquez

Fredrik Lundh said:
"transparency", not "trasparency".

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

-Ruben
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top