如何使用PIL(python-imaging)创建透明的gif(或png) [英] How to CREATE a transparent gif (or png) with PIL (python-imaging)

查看:472
本文介绍了如何使用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')

这篇关于如何使用PIL(python-imaging)创建透明的gif(或png)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆