MacOSX中的PyGame:CGContextDrawImage:无效的上下文0x0

时间:2016-10-24 03:05:56

标签: python pygame cgcontext cgcontextdrawimage

我最近开始使用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.

我已尝试在互联网上搜索遇到此问题的人,但我能找到的是人们在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:我尝试运行PyGame示例代码,加载PNG文件并显示它(pygame.examples.scaletest)并发生同样的错误,因此我猜测它是Sierra更新问题。< / p>

3 个答案:

答案 0 :(得分:2)

除了卸载pygame之外,我还必须卸载以下内容:

brew uninstall sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi

然后重新安装:

brew install sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi

之后:

python -m pip install pygame

它又开始工作了。

答案 1 :(得分:1)

升级到Sierra之后我遇到了同样的问题。 我的解决方案是卸载并重新安装PyGame。

答案 2 :(得分:0)

我在Sierra for python3中遇到了同样的问题。

  • 首先,我从3.5升级到3.6.1。
  • 其次,我降级了pygame。
  • 卸载pip3 uninstall pygame ==&gt;卸载1.9.4
  • 安装pip3 install pygame ==&gt;安装1.9.3