Skip to content

Instantly share code, notes, and snippets.

@h2suzuki
Last active March 2, 2017 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h2suzuki/e38a3cc3186f36a7fd0858b23bcbb44b to your computer and use it in GitHub Desktop.
Save h2suzuki/e38a3cc3186f36a7fd0858b23bcbb44b to your computer and use it in GitHub Desktop.
import numpy as np
import json
import zmq
def send_image(socket, image, channel = b"image"):
# Serialize a Numpy array
dtype = str(image.dtype).encode('ascii')
shape = json.dumps(image.shape).encode('ascii')
data = image.tostring('C')
socket.send_multipart([channel, dtype, shape, data])
def recv_image(socket, flags = 0):
channel, dtype, shape, data = socket.recv_multipart(flags)
# Deserialize a numpy array
image = np.frombuffer(data, dtype=dtype.decode('ascii'))
image.shape = json.loads(shape.decode('ascii'))
return image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment