我最近开始使用 PyGame 开发一个显示图像的简单程序,但我遇到了一个问题,我无法加载具有大量颜色的图像(?)。在我的第一次测试中,我使用了一些真实的分割图像,所以它是全黑的,两种颜色描绘了图像的分割部分。当我尝试绘制真实图像时,它会在终端中显示黑色图像和以下错误:
Oct 24 00:50:08 python[4371] <Error>: CGBitmapContextInfoCreate: CGColorSpace does't support output
Oct 24 00:50:08 python[4371] <Error>: CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
我尝试在 Internet 上搜索遇到此问题的人,但我只能找到在 XCode 上遇到无效上下文问题的人(我没有使用)。
为此,这是我编写的简单代码,并不神秘:
import pygame
import glob
import os
pygame.init()
screen = pygame.display.set_mode((640, 640))
imageName = '2.png'
myimage = pygame.image.load(imageName)
imagerect = myimage.get_rect()
while 1:
screen.fill((255,0,255))
screen.blit(myimage, imagerect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
我正在运行 Python 2.7.12、OSX 10.12 Sierra 和 PyGame 1.9.2。
编辑:我刚刚尝试了一个全黑的PNG文件,并且发生了同样的错误。我不知道发生了什么事。
编辑2:我尝试运行加载PNG文件并显示它的PyGame示例代码(pygame.examples.scaletest)并且发生相同的错误,所以我猜这是Sierra更新问题。