3

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.

bnoite
  • 185
  • 2
  • 12
  • The code you posted works for me on Sierra using python 2.7.12 installed from brew, combined with `sudo python -m pip install pygame` and `brew install libpng` – CodeSurgeon Oct 27 '16 at 01:51
  • @user2588654 What image you tried using on it? Only a few pictures work for me, maybe you got lucky. I've already updated my python. Thanks! – bnoite Oct 27 '16 at 20:24
  • I tried it with [this](http://python.net/~goodger/projects/graphics/python/newlogo-repro.png) python logo online image and it worked. Maybe provide a link to an image that is failing for you and I can test it out. Also, have you tried calling `pygame.image.load(imageName).convert()` or `.convert_alpha()` instead? Not sure if that would help but it does improve blit performance. – CodeSurgeon Oct 28 '16 at 01:22

3 Answers3

2

In addition to uninstalling pygame, I also had to uninstall the following:

brew uninstall sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi

Then reinstall:

brew install sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi

Then after:

python -m pip install pygame

It started working again.

joshaidan
  • 3,842
  • 1
  • 22
  • 17
1

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

mojo
  • 26
  • 4
0

I have the same issue in Sierra for python3.

  • First, I upgraded from 3.5 to 3.6.1.
  • Second, I downgrade the pygame.
  • Uninstall pip3 uninstall pygame ==> Uninstall 1.9.4
  • Install pip3 install pygame ==> Install 1.9.3
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Jaepyoung
  • 71
  • 1
  • 6