我试图通过 ZeroMQ 连接与 pyzmq 发送 jpeg 图像文件,但输出是输入大小的 3 倍,并且不再是有效的 jpeg。我加载图像并发送...
f = open("test1.jpg",'rb')
strng = f.read()
socket.send(strng)
f.close()
我接收并保存...
message = socket.recv()
f = open("test2.jpg", 'w')
f.write(str(message))
f.close()
我是 zmq 新手,我找不到任何有关发送图像的信息。有没有人通过 ZeroMQ 发送图像,或者对如何查找问题有任何想法?
最佳答案
在发送文件之前,您可以对其进行“base64”编码并在收到时对其进行解码。
发送:
import base64
f = open("test1.jpg",'rb')
bytes = bytearray(f.read())
strng = base64.b64encode(bytes)
socket.send(strng)
f.close()
接收:
import base64
message = socket.recv()
f = open("test2.jpg", 'wb')
ba = bytearray(base64.b64decode(message))
f.write(ba)
f.close()
关于image - ZeroMQ pyzmq通过tcp发送jpeg图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791633/