Python枕头/PIL颜色变换问题。GIF格式

Python枕头/PIL颜色变换问题。GIF格式,python,image,image-processing,python-imaging-library,Python,Image,Image Processing,Python Imaging Library,代码: 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 = []

代码:

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:])
我的GIF在添加其他PNG后会发生颜色变化

gif输出

二维码(无背景)


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

要修复颜色,请尝试以下操作:

  • 将基础图像转换为RGB(
    .Convert('RGB')
  • 添加PNG
  • 转换回调色板模式(
    .Convert('P')
  • 这样,PIL将生成一个新的调色板,更好地逼近组合图像中的所有颜色