将opengl中的图像数据编码为PIL image



我有一个字符列表,我从调用openGL中的glreadPixels得到。我想把这个字符串列表转换成一个图像,并在python中显示。这是我尝试过的:

st = ''.join((e)for e in get_frame_binary_data())
d = np.frombuffer(st.encode(),dtype=np.uint8,count=480*640*3).reshape([480,640,-1])   
plt.imshow(d)
plt.show()

我从中得到的结果是完全错误的。但是,如果我将相同的数据写入文本文件(在opengl c++中)并从python读取它,它会工作:

with open('frame_binary_data', mode='rb') as f:
d = np.frombuffer(f.read(),dtype=np.uint8,count=480*640*3).reshape([480,640,-1]) 
plt.imshow(d)
plt.show()

我猜这和编码有关。有人能帮我吗?

使用glreadpixels,您需要将数据保存为unsigned char,然后在python代码中,我使用bytes(my_frame_data)将列表转换为字节

d = np.frombuffer(bytes(get_frame_binary_data()),dtype=np.uint8,count=480*640*3).reshape([480,640,-1])  

可以工作,问题是我的数据需要保存为unsigned char

最新更新