在Python中将VNC RFB帧数据转换为Image

时间:2015-09-04 12:35:45

标签: python numpy scipy python-imaging-library scikit-image

我目前正在尝试从RFB协议(VNC连接)获取帧缓冲区更新,并使用Python的Pillow或scikit-image(numpy)将32位RAW(字节数据字符串)转换为图片我目前可以做它与PyQt4:

picture = PyQt4.QtGui.QImage(width, height, PyQt4.QtGui,QImage.Format_RGB32)
box = PyQt4.QtGui.QImage(box_w, box_h, PyQt4.QtGui,QImage.Format_RGB32)
with PyQt4.QtGui.QPainter(picture) as qp:
    qp.drawImage(x, y, box, 0, 0, x_val, y_val)

但是,我不想引入PyQt4库来执行此操作。如何使用Pillow或scikit-image(w / numpy)做同样的事情?

我尝试使用原始解码器使用Image.frombuffer()并将字节直接读入ndarray;但是当我在没有任何操作的情况下保存生成的对象/数组时,我可以看到它没有正确地接收字节。

1 个答案:

答案 0 :(得分:0)

我假设您的'32位RAW'图片只是一个字节缓冲区,其中每个四元组代表R,G,B,A格式的像素。您可以使用下面的代码从中创建枕头图像。我使用数组模块创建一个示例输入内存缓冲区。

from PIL import Image
import array

# Raw bytes representing a 2x2 image.
rawdata = array.array("B", (255, 0, 0, 255,    0, 255, 0, 255,
                            0, 0, 255, 255,  255, 255, 0, 255))

# Make a pillow image from the raw bytes.
im = Image.frombytes("RGBA", (2, 2), rawdata)
im.show()
im.save("test.png")