我正在尝试在我的程序中使用Pillow将从相机获取的字节字符串保存到文件中。这是一个例子,使用来自相机的小型原始字节字符串,应该代表一个分辨率为10x5像素、使用LSB和12位的灰度图像:
import io
from PIL import Image
rawBytes = b'_\x00`\x00[\x00^\x00`\x00`\x00\\\x00\\\x00Z\x00\\\x00_\x00[\x00\\\x00\\\x00`\x00]\x00\\\x00^\x00_\x00\\\x00\\\x00]\x00]\x00_\x00]\x00]\x00Z\x00\\\x00^\x00\\\x00Z\x00^\x00_\x00]\x00^\x00Z\x00\\\x00Z\x00\\\x00]\x00_\x00]\x00^\x00Z\x00[\x00[\x00X\x00]\x00]\x00Z\x00'
rawIO = io.BytesIO(rawBytes)
rawIO.seek(0)
byteImg = Image.open(rawIO)
byteImg.save('test.png', 'PNG')
然而,在第7行(使用
Image.open
)时,我遇到了以下错误:OSError: cannot identify image file <_io.BytesIO object at 0x00000000041FC9A8>
Pillow文档中给出的解决方法并不能解决问题。您可以尝试以下链接的解决方案,但是可能无法解决问题:为什么这个方法不能起作用呢?请帮忙检查一下。
Image.frombuffer("F", (10, 5), rawbytes, "raw", "F;16")
。 - smiddy84