如何使用PIL(python-imaging)创建透明的gif(或png)

如何使用PIL(python-imaging)创建透明的gif(或png)

问题描述:

尝试使用PIL 创建透明gif.到目前为止,我有这个:

Trying to create a transparent gif with PIL. So far I have this:

    from PIL import Image

    img = Image.new('RGBA', (100, 100), (255, 0, 0, 0))
    img.save("test.gif", "GIF", transparency=0)

到目前为止,我发现的所有内容都涉及对现有图像进行调整以调整其透明度设置或将透明图像覆盖到另一个图像上.我只想创建一个透明的GIF(然后绘制).

Everything I've found so far refers to manipulating an existing image to adjust it's transparency settings or overlaying a transparent image onto another. I merely want to create a transparent GIF (to then draw onto).

以下脚本创建一个透明GIF,中间画有一个红色圆圈:

The following script creates a transparent GIF with a red circle drawn in the middle:

from PIL import Image, ImageDraw

img = Image.new('RGBA', (100, 100), (255, 0, 0, 0))

draw = ImageDraw.Draw(img)
draw.ellipse((25, 25, 75, 75), fill=(255, 0, 0))

img.save('test.gif', 'GIF', transparency=0)

,对于PNG格式:

img.save('test.png', 'PNG')