Transparent PNG and PIL

Discussion in 'Python' started by R.Marquez, May 26, 2004.

  1. R.Marquez

    R.Marquez Guest

    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?
     
    R.Marquez, May 26, 2004
    #1
    1. Advertising

  2. 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>
     
    Fredrik Lundh, May 26, 2004
    #2
    1. Advertising

  3. R.Marquez

    R.Marquez Guest

    "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.
     
    R.Marquez, May 27, 2004
    #3
  4. "R.Marquez" wrote:

    > >>> image.info

    > {'transparency': 0}
    > >>> image.save("SomeT.png", trasparency=0)
    > >>>


    "transparency", not "trasparency".

    (the PNG writer ignores unknown configuration options)

    </F>
     
    Fredrik Lundh, May 27, 2004
    #4
  5. R.Marquez

    R.Marquez Guest

    "Fredrik Lundh" wrote:

    > "transparency", not "trasparency".


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

    -Ruben
     
    R.Marquez, May 28, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Eyles

    Transparent PNG

    Martin Eyles, Dec 1, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    651
    Martin Eyles
    Dec 2, 2004
  2. Andrea Fino

    transparent png on explorer

    Andrea Fino, Apr 13, 2004, in forum: HTML
    Replies:
    17
    Views:
    675
    Mark Parnell
    Apr 14, 2004
  3. Susanne
    Replies:
    8
    Views:
    611
    Tim Fitzgerald
    Aug 2, 2004
  4. Martin
    Replies:
    15
    Views:
    2,532
    Steve Pugh
    Jul 21, 2005
  5. Alex
    Replies:
    1
    Views:
    716
    Steve W. Jackson
    May 2, 2006
Loading...

Share This Page