我正在尝试使用枕头在我的程序中保存一个字节串从我的相机到一个文件。下面是一个示例,它使用LSB和12位来表示分辨率为10x5像素的灰度图像:
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>
来自枕头的文档暗示这是一条道路。
我试着应用
但不能让它起作用。为什么这不管用?
发布于 2015-08-25 17:44:59
我不确定生成的图像应该是什么样子(有示例吗?),但是如果要将每个像素有12位的压缩图像解压缩为16位图像,可以使用以下代码:
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'
im = Image.frombuffer("I;16", (5, 10), rawbytes, "raw", "I;12")
im.show()
https://stackoverflow.com/questions/32208612
复制相似问题