3

I used to use Python 2.7 and pygame and, to load a PNG and blit it to a surface, this code was enough:

sprites = pygame.image.load('sprites.png')
background.blit(sprites, (0, 0), rect)

Now I'm using Python 3, which means I need to load PIL to deal with PNG files. My new code looks like this:

from PIL import Image
sprites = Image.open('sprites.png')
background.blit(sprites, (0, 0), rect)

But when I run the new code, I get this error on the last line:

TypeError: argument 1 must be pygame.Surface, not PngImageFile

So the sprites variable is a PngImageFile and background.blit() is expecting a pygame.Surface object as input:

  • Should I convert the PngImageFile to a pygame.Surface? I can't find any way to do this.
  • Is there a better way to blit a PIL Image to a pygame.Surface object?

I've tried hard looking for an answer on StackOverflow, so I'm pretty sure this is not a duplicate question. Any comments or suggestions would be very welcome.

3
  • Sorry, but HOW did you get PIL to work with Python3.. They havn't even released it yet? o0
    – Torxed
    Feb 20, 2014 at 13:48
  • Also, load the image into a pygame image from a string: pygame.org/docs/ref/image.html#pygame.image.fromstring
    – Torxed
    Feb 20, 2014 at 13:50
  • 1
    There is a fork, called pillow, it seems to work, let me know if you need further details
    – user1115363
    Feb 20, 2014 at 14:01

1 Answer 1

2
from PIL import Image
sprites = Image.open('sprites.png')
pygameImage = pygame.image.fromstring(str(buffer(sprites)), win.size, 'RGB', False)
background.blit(pygameImage, (0, 0), rect)

Since i'm running Python3 without any luck on PIL i'm not entirely sure this will work out of the box. Because you need to get the text-representation of the sprites object instead of the array of data.

Following might help you to convert a PIL object into a string?

Or simply try:

from PIL import Image
sprites = pygame.image.load(Image.open('sprites.png'))
background.blit(sprites, (0, 0), rect)
3
  • The third line, slightly modified, at the moment is like this: pygameImage = pygame.image.fromstring(sprites.tostring(), len(sprites.tostring()), 'RGB', False) But I get this: TypeError: must be 2-item sequence, not int Any idea?
    – user1115363
    Feb 20, 2014 at 14:26
  • @hertsmael Probably because size=... (if you read the docs on pygame.image.fromstring requires width+height, not the length of the string that represents the image). Try doing pygameImage = pygame.image.fromstring(sprites.tostring(), (sprites.width, sprites.height), 'RGB', False) instead.
    – Torxed
    Feb 20, 2014 at 14:30
  • As I could't find the right methods in PngImageFile object to get width and height I juust set them hardcoded (just to try making it work). The error I get is: pygameImage = pygame.image.fromstring(sprites.tostring(), (760, 166), 'RGB', False) ValueError: String length does not equal format and resolution size pastebin.com/QpuEwPc0
    – user1115363
    Feb 20, 2014 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.