gpt4 book ai didi

python - 如何用pygame显示PIL图像?

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:10 25 4
gpt4 key购买 nike

我正在尝试通过 wifi 从我的 raspberry pi 做一些视频流。我使用了 pygame,因为我还必须在我的项目中使用游戏 handle 。不幸的是,我坚持显示接收到的帧。很快:我得到 jpeg 帧,用 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

最佳答案

对于较新版本的 Pygame,Sloth 的回答是不正确的。 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')

关于python - 如何用pygame显示PIL图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33244928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com