鱼C论坛

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

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

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

最佳答案

查看完整内容

本身好像不支持,但你可以看看这个 https://blog.csdn.net/m0_54853787/article/details/138930978 https://www.cnblogs.com/xingnie/p/15269808.html
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-3-9 13:44:34 | 显示全部楼层    本楼为最佳答案   
本身好像不支持,但你可以看看这个
https://blog.csdn.net/m0_54853787/article/details/138930978
https://www.cnblogs.com/xingnie/p/15269808.html
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-3-9 13:44:53 | 显示全部楼层
Pygame 是否支持 GIF 图片?

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

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

  1. import pygame
  2. from PIL import Image

  3. def load_gif_frames(gif_path):
  4.     im = Image.open(gif_path)
  5.     frames = []
  6.     try:
  7.         while True:
  8.             pygame_frame = pygame.image.fromstring(im.tobytes(), im.size, im.mode)
  9.             frames.append(pygame_frame)
  10.             im.seek(im.tell() + 1)
  11.     except EOFError:
  12.         pass
  13.     return frames

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

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

  28. pygame.quit()
复制代码


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


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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

甚至比ai快
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-12 18:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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