How to display PIL image with pygame?

I'm trying to make some video stream from my raspberry pi over wifi. I used pygame because I also need to use a gamepad in my project. Unfortunately, I am stuck in displaying the resulting frame. Soon: I get a jpeg frame, open it with PIL, convert to a string - after that I can load an image from a string

image_stream = io.BytesIO()
...
frame_1 = Image.open(image_stream) 
f = StringIO.StringIO()
frame_1.save(f, "JPEG")
data = f.getvalue()
frame = pygame.image.fromstring(frame_1,image_len,"RGB")
screen.fill(white)
screen.blit(frame, (0,0))
pygame.display.flip()

and error:

Traceback (most recent call last):
  File "C:\Users\defau_000\Desktop\server.py", line 57, in <module>
    frame = pygame.image.fromstring(frame_1,image_len,"RGB")
TypeError: must be str, not instance
+4
source share
2 answers

The first argument pygame.image.fromstringshould be str.

So, when frame_1is your PIL image, convert it to a string with tostringand load that string with pygame.image.fromstring.

, .

raw_str = frame_1.tostring("raw", 'RGBA')
pygame_surface = pygame.image.fromstring(raw_str, size, 'RGBA')
+2

Pygame. tostring() . Python 3.6, PIL 5.1.0, Pygame 1.9.3:

raw_str = frame_1.tobytes("raw", 'RGBA')
pygame_surface = pygame.image.fromstring(raw_str, size, 'RGBA')
0

Source: https://habr.com/ru/post/1612489/


All Articles