如何加载动画 GIF 并获取 Pygame 中的所有单个帧? [英] How can I load an animated GIF and get all of the individual frames in Pygame?

查看:32
本文介绍了如何加载动画 GIF 并获取 Pygame 中的所有单个帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起,如果这是重复的.我找到的答案似乎无关紧要,但也许我正在使用错误的关键字进行搜索.我想要做的是获取动画 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

这篇关于如何加载动画 GIF 并获取 Pygame 中的所有单个帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆