使用 Pyhton PIL 从动画 GIF 获取调色板 [英] Get color palette from animated GIF with Pyhton PIL
问题描述
我想获取动画 GIF 的每一帧的调色板(我使用 ColorThief 从图像生成调色板).我正在使用 PIL 将每一帧保存为图像,我的问题是大多数帧的图像都有奇怪的颜色,与您在浏览器上打开 GIF 时看到的颜色不同.我想这是因为动画 GIF 的压缩方式.我试图以 RGB 格式转换每个图像,但这并不能解决问题.如何使用预期的颜色渲染每个帧?
I'd like to get the color palette of each frame of an animated GIF (I'm using ColorThief to generate color palette from an image). I'm using PIL to save each frame as an image, my problem is that the images of most of the frames have strange colors, different from the one you see if you open the GIF on a browser. I guess this is because of the way animated GIF are compressed. I tried to convert every image in RGB but this doesn't solve the problem. How can I render each frame with its intended colors?
要获取每一帧,我使用以下代码:
To get every frame I'm using the following code:
from PIL import Image, ImageSequence
im = Image.open("phicons/icontech-3-2019-1.gif")
index = 1
for frame in ImageSequence.Iterator(im):
frame.save("phicons/frame%d.gif" % index)
print(frame)
index += 1
这是我用于测试的图像
这是一个带有奇怪颜色的框架的例子
This is an example of frame with strange colors
推荐答案
可以试试 Python libary moviepy一>.
You can try Python libary moviepy.
from moviepy.editor import VideoFileClip
import cv2
import numpy as np
clip = VideoFileClip('sample.gif')
idx = 0
for frame in clip.iter_frames(): # frame is in rgb format
cv2.imwrite('sample-{}.png'.format(idx), frame[:,:,::-1])
这篇关于使用 Pyhton PIL 从动画 GIF 获取调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!