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

时间:2023-01-24 14:57:27

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

我只是想用pygame来加载一个图像

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

And I get the error :

我得到了误差:

pygame.error: File is not a Windows BMP file

pygame。错误:文件不是一个Windows BMP文件。

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

注意,当我调用pygame.image.get_extended()时,它返回0。我该如何修复和获取其他格式呢?

Thanks

谢谢

1 个解决方案

#1


2  

The Pygame documentation for images explicitly says

图像的Pygame文档明确表示。

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.

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

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.

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

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

或者,您也可以将图像转换为BMP以避免麻烦。

#1


2  

The Pygame documentation for images explicitly says

图像的Pygame文档明确表示。

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.

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

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.

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

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

或者,您也可以将图像转换为BMP以避免麻烦。