【问题标题】:How to CREATE a transparent gif (or png) with PIL (python-imaging)如何使用 PIL(python 成像)创建透明 gif(或 png)
【发布时间】:2012-01-12 15:57:19
【问题描述】:

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

    from PIL import Image

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

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

【问题讨论】:

    标签: python python-imaging-library transparency gif imaging


    【解决方案1】:

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

    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')
    

    【讨论】:

    • 只要将color参数中的第4个值(在本例中为(255, 0, 0, 0))设置为0,图像就会完全透明——这是alpha通道的值.所有这些都应该完全相同:(255, 255, 255, 0)(0, 0, 0, 0)(100, 100, 100, 0)
    • 对于动画 GIF,我必须使用以下代码:gist.github.com/egocarib/ea022799cca8a102d14c54a22c45efe0
    猜你喜欢
    • 2011-10-19
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2011-05-21
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多