且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 pygame.image 加载 PNG 文件?

更新时间:2023-11-10 13:32:52

Pygame 文档图片明确表示

图像模块是 Pygame 的必需依赖项,但它仅可选地支持任何扩展文件格式.默认情况下,它只能加载未压缩的 BMP 图像.

所以,我想你应该跑

pygame.image.get_extended() # 返回一个布尔值

检查您是否可以加载其他扩展程序的图像.如果没有,我想您将需要安装 Python 映像库才能获得 Pygame 支持的扩展文件格式.

或者,您可以随时将图像转换为 BMP 以避免麻烦.

I'm simply trying to load an image with pygame with that piece of code :

import pygame
myimage = pygame.image.load("bla.png")

And I get the error :

pygame.error: File is not a Windows BMP file

Note that when I call pygame.image.get_extended() it returns 0. How can I fix that and get the other formats ?

Thanks

The Pygame documentation for images explicitly says

The image module is a required dependency of Pygame, but it only optionally supports any extended file formats. By default it can only load uncompressed BMP images.

So, I suppose you should run

pygame.image.get_extended() # returns a bool

to check if you can load images of other extensions. If not, I suppose you will need Python imaging libraries to be installed to get extended file formats to be supported by Pygame.

OR, you could always convert the images to BMP to avoid the hassle.