Python用PIL将PNG图像合成gif时如果背景为透明时图像出现重影的解决办法

最近在用PIL合成PNG图像为GIF时,因为需要透明背景,所以就用putpixel的方法替换背景为透明,但是在合成GIF时,图像出现了重影,在网上查找了GIF的相关资料:GIF相关资料 其中有对GIF帧数处理的说明,需要在GIF图像的header中设置disposal的处置方法

Python用PIL将PNG图像合成gif时如果背景为透明时图像出现重影的解决办法_第1张图片

然后我们可以查看PIL库中关于GIF的定义文件GifImagePlugin.py

disposal = int(im.encoderinfo.get("disposal", 0))

    if transparent_color_exists or duration != 0 or disposal:
        packed_flag = 1 if transparent_color_exists else 0
        packed_flag |= disposal << 2
        if not transparent_color_exists:
            transparency = 0

        fp.write(
            b"!"
            + o8(249)  # extension intro
            + o8(4)  # length
            + o8(packed_flag)  # packed fields
           

你可能感兴趣的:(python,PIL)