简体   繁体   English

BytesIO对象到图像

[英]BytesIO object to image

I'm trying to use Pillow in my program to save a bytestring from my camera to a file. 我正在尝试在程序中使用Pillow将一个字节串从我的相机保存到文件中。 Here's an example with a small raw byte string from my camera which should represent a greyscale image of resolution 10x5 pixels, using LSB and 12bit: 这是一个示例,我的相机有一个小的原始字节字符串,它应该代表一个分辨率为10x5像素的灰度图像,使用LSB和12bit:

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')

However I get the following error in line 7 (with Image.open ): 但是我在第7行中遇到以下错误(使用Image.open ):

OSError: cannot identify image file <_io.BytesIO object at 0x00000000041FC9A8>

The docs from Pillow implied this was the way to go. Pillow的文档暗示这是要走的路。

I tried to apply the solutions from 我试图应用解决方案

but can't get it working. 但无法让它发挥作用。 Why isn't this working? 为什么这不起作用?

I'm not sure what the resulting image should look like (do you have an example?), but if you want to unpack an packed image where each pixel has 12 bits into a 16-bit image, you could use this code: 我不确定结果图像应该是什么样的(你有一个例子吗?),但是如果你想解压缩一个打包的图像,其中每个像素有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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM