5

I'm trying to load .png images into a program using PIL.Image, so that I can manipulate them, ready for use as pygame surfaces in sprites. The following code shows how I've tried to convert those Pil Images into pygame images :

bytes = someImagefile.tobytes()
new_image = pygame.image.fromstring(bytes, size, "RGB")

I'm getting : "ValueError: String length does not equal format and resolution size"

Is there a way to do this without saving a new .png copy after I'm done with playing with it?

user3731111
  • 65
  • 1
  • 6

1 Answers1

9

The following code works for me. Python2.7+PIL 2.5+Pygame1.9.2

import Image
import pygame
image = Image.open("SomeImage.png")


mode = image.mode
size = image.size
data = image.tobytes()

py_image = pygame.image.fromstring(data, size, mode)
Saurav Gupta
  • 535
  • 3
  • 11