Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BytesIO对象到图像_Python_Python 3.x_Io_Python Imaging Library - Fatal编程技术网

Python BytesIO对象到图像

Python BytesIO对象到图像,python,python-3.x,io,python-imaging-library,Python,Python 3.x,Io,Python Imaging Library,我正试图在我的程序中使用枕头将bytestring从我的相机保存到一个文件中。下面是一个示例,其中有一个来自我相机的小原始字节字符串,它应该表示分辨率为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\\\x

我正试图在我的程序中使用枕头将bytestring从我的相机保存到一个文件中。下面是一个示例,其中有一个来自我相机的小原始字节字符串,它应该表示分辨率为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:无法识别图像文件
来自美国的文件暗示这是一条路要走

我试着应用来自世界各地的解决方案


但是它不能工作。为什么不起作用?

我不确定生成的图像应该是什么样子(你有例子吗?),但是如果你想将每个像素有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()

谢谢你的提示!在我的例子中,输出是一个Windows字类型(所以是16位小尾端无符号整数)。最后,通过查看,我能够正确地解码我的图像:
image.frombuffer(“F”,(10,5),rawbytes,“raw”,“F;16”)
我很高兴frombuffer()函数满足了您的要求,我的建议使您走上了正确的轨道。但是,我必须深入研究枕头的源代码才能找到这个解决方案;-)
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()