【问题标题】:Python Pillow/PIL color shifting issue. In GIFPython Pillow/PIL 变色问题。在 GIF 中
【发布时间】:2020-12-30 22:25:56
【问题描述】:

代码:https://gist.github.com/jksfsfjk23h4/d95df9778ce6769873d584349d4f24a3

animated_gif = Image.open("trippy.gif")

#getting size
x_qr = animated_gif.size[0]
y_qr = animated_gif.size[1]

img__ = Image.new('RGBA', (x_qr, y_qr), (0, 0, 0, 0))
resized_picture_1 = qrAdder(img__,"http://ke")

frames = []
for frame in ImageSequence.Iterator(animated_gif):
    frame = frame.copy()
    frame.convert('RGBA')
    # resized_picture_1 = qrAdde    r(frame,"http://ke")
    frame.paste(resized_picture_1, (0,0),resized_picture_1)
    frame.convert('RGB')
    frames.append(frame)

frames[0].save('wowi.gif', save_all=True, append_images=frames[1:])

在我添加其他 PNG 后,我的 GIF 会发生颜色变化

gif 输出

二维码(无背景)

【问题讨论】:

    标签: python image image-processing python-imaging-library


    【解决方案1】:

    GIF 格式使用 8 位调色板 (Wikipedia) 表示颜色。 GIF 帧中不同颜色的最大数量为 256。一个调色板条目通常用于“清除”动画,减少到 255 种不同颜色。当您添加 PNG 图像时,PIL 将其转换为使用调色板中最接近的可用颜色,但显然调色板中的任何颜色都没有那么接近。

    要修复颜色,试试这个:

    1. 将基本图像转换为 RGB (.convert('RGB'))。
    2. 添加 PNG。
    3. 转换回调色板模式 (.convert('P'))。

    这样,PIL 将生成一个新的调色板,可以更好地近似组合图像中的所有颜色。

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2014-01-27
    • 2017-01-03
    • 1970-01-01
    • 2012-02-21
    • 2019-04-26
    相关资源
    最近更新 更多