[Image-SIG] GIF or PNG transparency

Scott Russell scott@portalsmith.net
Fri, 9 Feb 2001 12:48:47 -0500


I don't think this made it to the mailing list, but I wanted to make sure it
was stored in the archives for anyone else who may need this.


> Scott Russell wrote:
> > I was starting to figure that was the problem, but IE shows *some* png
files
> > fine - lipng.org has lots that work ok - how do I save in whatever
format it
> > does like?
>
> P (palette) images seem to work fine, RGBA images don't.
>
> :::
>
> To create palette PNG's, you need:
>
> 1) an updated version of the PngImagePlugin file, which I
> just posted to http://effbot.org/pil
>
> 2) create a "P" image, and attach a palette to it.  see this
> article for details:
>
http://www.pythonware.com/products/pil/articles/creating-palette-images.htm
>
> 3) pass the "transparency" option to the save method.  here's
> an example using ImageDraw:
>
> import Image
> import ImageDraw
>
> im = Image.new("P", (400, 400), 0)
>
> im.putpalette([
>     0, 0, 0, # transparent/black background
>     255, 0, 0, # index 1 is red
>     255, 255, 0, # index 2 is yellow
>     255, 153, 0, # index 3 is orange
>     ])
>
> d = ImageDraw.ImageDraw(im)
> d.setfill(1)
>
> d.setink(1)
> d.polygon((0, 0, 0, 400, 400, 400))
>
> d.setink(3)
> d.rectangle((100, 100, 300, 300))
>
> d.setink(2)
> d.ellipse((120, 120, 280, 280))
>
> im.save("out.png", transparency=0)
>
> :::
>
> to do the same for an existing image, you can do some-
> thing like this:
>
>     mask = ...extract from image...
>     im = image.convert("P", palette=Image.ADAPTIVE, colors=255)
>     im.paste(255, None, mask)
>     im.save(filename, transparency=255)
>
> :::
>
> Hope this helps!
>
> Cheers /F
>
>
>