PIL and transparent GIFs

T

toto

Hi,

Does anybody know how to save an image in GIF format preserving transparent
background ??

Here's what I tested :

import Image, ImageDraw
im = Image.open('/path/to/model.gif') # An image with transparent backgroung
draw = ImageDraw.Draw(im)
draw.polygon([153,106,186,225,340,193,315,81,304,167], fill=9587)
im.save('/path/to/model1.gif', 'GIF')

And model1.gif has no transparent background anymore even if it's bigger
than my polygon selection.

Regards,

Laurent.
 
K

Kamilche

It can only read transparency, it can't write it. I went looking and
found that out a couple weeks ago.
 
T

Terry Hancock

It can only read transparency, it can't write it. I went
looking and found that out a couple weeks ago.

There was a patch published at one time that was supposed
to fix this. I remember doing some testing and not finding
it to work perfectly, but I may have been doing something
wrong. Can't recover it at the moment, but some search
engine work might turn it up.
 
F

Fredrik Lundh

Terry said:
There was a patch published at one time that was supposed
to fix this. I remember doing some testing and not finding
it to work perfectly, but I may have been doing something
wrong. Can't recover it at the moment, but some search
engine work might turn it up.

transparency write support was added in 2002.

however, PIL's Image object doesn't have a notion of "transparent
color index", so you have to keep track of it yourself.

im = Image.open(...)
transparency = im.info["transparency"]
...
out.save("out.gif", transparency=transparency)

</F>
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top