Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ pyzmq send jpeg image over tcp

I am trying to send a jpeg image file through a ZeroMQ connection with pyzmq, but the output is 3 times the size of the input, and no longer a valid jpeg. I load the image and send with...

f = open("test1.jpg",'rb')
strng = f.read()
socket.send(strng)
f.close()

I receive and save with...

message = socket.recv()
f = open("test2.jpg", 'w')
f.write(str(message))
f.close()

I am new to zmq, and I could not find any info on sending images. Has anyone sent images through ZeroMQ, or have any ideas on how to find the problem?

like image 562
user3325088 Avatar asked Jul 16 '14 22:07

user3325088


People also ask

Is ZeroMQ asynchronous?

ZeroMQ is an asynchronous network messaging library known for its high performance. It's intended use is for distributed systems as well as concurrent systems. In summary, ZMQ allows you to send messages (binary data, serialized data, simple strings, etc.)

Does ZeroMQ use sockets?

ZeroMQ patterns are implemented by pairs of sockets with matching types. The built-in core ZeroMQ patterns are: Request-reply, which connects a set of clients to a set of services. This is a remote procedure call and task distribution pattern.

What is imageZMQ?

imageZMQ is an easy to use image transport mechanism for a distributed image processing network. For example, a network of a dozen Raspberry Pis with cameras can send images to a more powerful central computer.

Why use ZeroMQ?

One of the effects of using ZeroMQ at large scale is that because we can build distributed architectures so much faster than before, the limitations of our software engineering processes become more visible. Mistakes in slow motion are often harder to see (or rather, easier to rationalize away).


2 Answers

Before sending the file you can "base64" encode it and decode it when received.

Sending:

import base64
f = open("test1.jpg",'rb')
bytes = bytearray(f.read())
strng = base64.b64encode(bytes)
socket.send(strng)
f.close()

Receiving:

import base64
message = socket.recv()
f = open("test2.jpg", 'wb')
ba = bytearray(base64.b64decode(message))
f.write(ba)
f.close()
like image 110
jschiavon Avatar answered Oct 22 '22 03:10

jschiavon


You can try imagezmq. It's specially built for transporting images using PyZMQ messaging.

Sender

import socket
import imagezmq

sender = imagezmq.ImageSender(connect_to='tcp://receiver_name:5555')

sender_name = socket.gethostname() # send your hostname with each image

image = open("test1.jpg",'rb')
sender.send_image(sender_name, image)

Receiver

import imagezmq

image_hub = imagezmq.ImageHub()

sender_name, image = image_hub.recv_image()
image_hub.send_reply(b'OK')
like image 2
rajput Avatar answered Oct 22 '22 03:10

rajput