问题描述
首先,对不起,如果重复的话.我发现的答案似乎无关紧要,但也许我在搜索错误的关键字.我想做的是制作一个动画GIF,并将其拆分为帧列表.基本上是这样的:
First of all, sorry if this is a duplicate. The answers that I found either seemed irrelevant but perhaps I'm searching with the wrong keywords. What I'd like to do is to take an animated GIF and split it up into a list of frames. Basically, something like this:
frames = []
for frame in split_animated_gif("some_animated_gif.gif"):
frames.append(frame)
其中split_animated_gif按顺序返回一个表面列表,每个表面都是GIF的框架.谢谢您的帮助.
where split_animated_gif returns a list of surfaces, each of which is a frame of the GIF, in order. Thank you for your help.
经过更多的侦听,我发现了一段代码,为我成功地在pygame中显示了动画GIF.可以在 https://github.com/piantado/kelpy/中找到blob/master/kelpy/GIFImage.py .不过,非常感谢您的帮助.
After some more snooping, I found a piece of code that successfully displayed an animated GIF in pygame for me. It can be found at https://github.com/piantado/kelpy/blob/master/kelpy/GIFImage.py. Your help was much appreciated, though.
推荐答案
Pygame本身不支持动画gif,这在文档中有说明.所以你必须
Pygame itself does not support animated gifs, which is stated in the documentation. So you'll have to
1)在加载精灵或图像时,使用其他代码/库来分割gif.通过谷歌搜索"python split gif"之类的东西,您会发现那些.例如. Python:将GIF帧转换为PNG
1) use some other piece of code / library to split the gif when you're loading your sprites or images. Those you would find just by googling for "python split gif" or something. E.g. Python: Converting GIF frames to PNG
2)如果您自己创建了gif,只需再次逐帧导出精灵
2) if you created the gif yourself, just export your sprites again frame by frame
无论哪种方式,您都必须手工制作动画部分.通过谷歌搜索"pygame动画",您会发现很多教程.例如. 来自少量图像的动画精灵
Either way, you'll have to do the animating part by hand. Which for you'll find plenty of tutorials by googling "pygame animation". E.g. Animated sprite from few images
这篇关于如何在Pygame中加载动画GIF并获取所有单独的帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!