pythonpygamecgcontextcgcontextdrawimage

PyGame in MacOSX: CGContextDrawImage: invalid context 0x0


I've recently started using PyGame to develop a simple program that shows an image but I ran into a problem where I can't load images with a lot of colour (?). During my first tests I was using some ground truth segmentation images, so it was all black and two colours delining the segmented portion of the image. When I tried drawing the real image, it would show up a black image and the following error in terminal:

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.

I've tried searching around the Internet for people that have ran into this problem but all I could find is people having the invalid context problem on XCode (which I'm not using).

For the sake of it, here's the simple code I wrote, no mystery:

    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) 

I'm running Python 2.7.12, OSX 10.12 Sierra and PyGame 1.9.2.

Edit: I just tried with a PNG file that's all black and the same errors occurs. I don't know what is going on.

Edit 2: I tried running PyGame example code that loads a PNG file and displays it (pygame.examples.scaletest) and the same error happens, so I'm guessing it's a Sierra update problem.


Solution

  • I had the same problem after upgrading to Sierra. My solution was to uninstall and reinstall PyGame.