鱼C论坛

 找回密码
 立即注册
查看: 25|回复: 3

[已解决]pygame支持gif图片吗

[复制链接]
发表于 昨天 13:44 | 显示全部楼层 |阅读模式
20鱼币
问题在标题

最佳答案

查看完整内容

本身好像不支持,但你可以看看这个 https://blog.csdn.net/m0_54853787/article/details/138930978 https://www.cnblogs.com/xingnie/p/15269808.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 昨天 13:44 | 显示全部楼层    本楼为最佳答案   
本身好像不支持,但你可以看看这个
https://blog.csdn.net/m0_54853787/article/details/138930978
https://www.cnblogs.com/xingnie/p/15269808.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 昨天 13:44 | 显示全部楼层
Pygame 是否支持 GIF 图片?

Pygame 本身不直接支持 GIF 动画。然而,你可以使用其他库来加载和显示 GIF。例如,你可以使用 Pillow 库来处理 GIF 的每一帧,然后在 Pygame 中逐帧显示。

以下是一个简单的示例,演示如何使用 Pillow 结合 Pygame 显示 GIF:
import pygame
from PIL import Image

def load_gif_frames(gif_path):
    im = Image.open(gif_path)
    frames = []
    try:
        while True:
            pygame_frame = pygame.image.fromstring(im.tobytes(), im.size, im.mode)
            frames.append(pygame_frame)
            im.seek(im.tell() + 1)
    except EOFError:
        pass
    return frames

pygame.init()
screen = pygame.display.set_mode((640, 480))
frames = load_gif_frames('your_gif.gif')
running = True
index = 0

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    screen.blit(frames[index], (0, 0))
    pygame.display.flip()
    index = (index + 1) % len(frames)
    pygame.time.delay(100)  # 调整帧率

pygame.quit()

要运行这个代码,你需要先安装 Pillow 库:
pip install Pillow

这个方法可以让你在 Pygame 中渲染 GIF,虽然它需要你自己管理帧切换和显示。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 昨天 14:31 | 显示全部楼层
sfqxx 发表于 2025-3-9 13:44
本身好像不支持,但你可以看看这个
https://blog.csdn.net/m0_54853787/article/details/138930978
https ...

甚至比ai快
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-3-10 03:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表