简体   繁体   English

将ByteArray转换为Python中从SoftwareBitmap框架在C#Windows.Graphic.Imaging上接收的图像文件

[英]Convert ByteArray to Image File received in Python from SoftwareBitmap frame on C# Windows.Graphic.Imaging

I have a requirement to get the VideoFrame.SoftwareBitMap from the Windows.Media.VideoFrame in C# and then convert this SOftwareBitmap to a ByteArray and then send the ByteArray using ZeroMQ to Python (using ZMQ) using TCP socket connection. 我需要从C#中的Windows.Media.VideoFrame获取VideoFrame.SoftwareBitMap ,然后将此SOftwareBitmap转换为ByteArray,然后使用TCP套接字连接使用ZeroMQ将ByteArray发送到Python(使用ZMQ)。

I am receiving the Bytearray from C# to Python but i am not able to convert this ByteArray in Python to an image . 我从C#接收Bytearray到Python,但我无法将Python中的ByteArray转换为图像。 I have tried the below code in python - 我在python中尝试了下面的代码 -

message = socket.recv_multipart()
    print('message2 received' ,message[2]) 
    img = cv2.imdecode(message[2], -1)

message[2] is the entire bytearray. message [2]是整个bytearray。

I receive the below error on the cv2.imdecode line - 我在cv2.imdecode行收到以下错误 -

TypeError: buf is not a numpy array, neither a scalar TypeError:buf不是一个numpy数组,也不是标量

Please advise what am i doing wrong ? 请告诉我我做错了什么?

Example of the ByteArray (some part) received - 收到的ByteArray(部分)示例 -

14\x1d\xff\n\x13\x1c\xff\n\x14\x1b\xff\t\x13\x1a\xff\x06\x11\x15\xff\x08\x13\x17\xff\x07\x13\x15\xff\x0c\x15\x18\xff\x0e\x17\x1a\xff\r\x14\x17\xff\x0e\x15\x18\xff\x12\x17\x1a\xff\x10\x14\x19\xff\x12\x14\x1c\xff\x12\x15\x1d\xff\x11\x16\x1f\xff\x0e\x17!\xff\x0c\x18$\xff\x0c\x18$\xff\x0c\x18"\xff\x0c\x19!\xff\x15\x1e\'\xff\x18 \'\xff\x19\x1f$\xff\x1f%,\xff\x1f$-\xff\x1e%.\xff!*4\xff\x1c%3\xff\x1e\';\xff#+B\xff&.E\xff&.E\xff#.D\xff!,B\xff\x1f*@\xff\x1e)?\xff (?\xff\x1e\';\xff %:\xff!\':\xff#%7\xff!#5\xff""2\xff$$2\xff\x1f *\xff\x17\x1a\x1f\xff\x17\x18\x1c\xff\x13\x15\x16\xff\x0e\r\x0f\xff\x07\x07\x07\xff\x00\x00\x00\xff\x01\x03\x03\xff\x04\x03\x05\xff\x01\x00\x02\xff\x04\x00\x03\xff\x06\x00\x02\xff\r\x00\x02\xff\x11\x01\x02\xff\x17\x03\x02\xff\x1a\x05\x03\xff\x18\x05\x00\xff\x19\x06\x00\xff\x1f\x08\x00\xff$\x08\x01\xff)\n\x01\xff.\n\x02\xff0\x0c\x02\xff0\r\x03\xff-\x0b\x05\xff%\t\x02\xff\x1b\x05\x00\xff\x10\x01\x00\xff\t\x00\x00\xff\x04\x00\x00\xff\x00\x00\x01\xff\x00\x00\x01\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x01\x01\x01\xff\x01\x01\x01\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\

Many Thanks, Prakhar 非常感谢,Prakhar

Save it as binary: 将其另存为二进制:

outimg = open('out.jpg', wb)
outimg.write(message[2])
outimg.close()

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

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