You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PIL保存无损WebP动画时右下角像素变黑问题咨询

问题解答

问题确认

这确实是Pillow库在处理无损WebP动画时的已知Bug,主要出现在9.x版本区间,表现为动画帧右下角的像素偶尔被错误渲染为黑色,触发概率较高但并非100%出现。

解决方案

1. 升级Pillow到最新稳定版

该Bug在Pillow 10.0.0及以上版本中已被官方修复,直接执行升级命令即可:

pip install --upgrade pillow

升级后原代码无需修改,保存的WebP动画将不会出现该问题。

2. 临时兼容方案(无法升级时)

如果受环境限制无法升级库,可以在保存前手动修正右下角像素:

from PIL import Image

images = []
for png_file in png_files:
    image = Image.open(png_file).convert('RGBA')
    width, height = image.size
    if width > 1 and height > 1:
        # 取倒数第二行倒数第二个像素的值替换右下角像素
        pixel_value = image.getpixel((width-2, height-2))
        image.putpixel((width-1, height-1), pixel_value)
    images.append(image)

# 保存动画
images[0].save("animation.webp", save_all=True, append_images=images[1:], duration=20, lossless=True)

验证方式

保存WebP动画后,可逐帧检查右下角像素是否正常:

from PIL import Image

with Image.open("animation.webp") as im:
    try:
        while True:
            width, height = im.size
            pixel = im.getpixel((width-1, height-1))
            print(f"Frame {im.tell()}: 右下角像素值 {pixel}")
            im.seek(im.tell()+1)
    except EOFError:
        pass

若所有帧的右下角像素均与原图像一致,则问题解决。

内容的提问来源于stack exchange,提问作者fabpub

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 21:53:13