2015-10-20 70 views
4

我想通過wifi從我的覆盆子pi做一些視頻流。我使用pygame,因爲我也必須在我的項目中使用遊戲手柄。不幸的是我對顯示收到的幀感到困惑。不久,我得到JPEG幀,與PIL打開它,轉換成字符串 - 之後,我可以從字符串如何用pygame顯示PIL圖像?

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() 

和錯誤加載圖像:

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 

回答

2

的第一個參數pygame.image.fromstring必須是一個str

因此當frame_1是您的PIL圖像時,將其轉換爲tostring的字符串,並加載該字符串與pygame.image.fromstring

您必須知道此圖片的大小才能生效。

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