0

I have a Tornado web-service with an API call that is supposed to accept an image and return certain segments of it to the client.

I have already managed to send a single image back to the client for a different API call like this:

byteIO = io.BytesIO()
Image.fromarray(preprocessedImg).save(byteIO, 'PNG')
self.write(byteIO.getvalue())
self.set_header("Content-type", "image/png")

I then tried sending multiple images this way:

results = {}
# changes the segments to raw image data
for key, segment in segments.items():
  byteIO = io.BytesIO()
  Image.fromarray(segment).save(byteIO, 'PNG')
  results[key] = byteIO.getvalue()
# sends the segments to the client
self.write(results)
# self.set_header("Content-type", "image/png")

this raises an error ".......x0c"\x91\t\x00\x00\x00\x00IEND\xaeB`\x82' is not JSON serializable " I get the same error when the self.set_header isn't commented out.

What is the correct way of sending multiple images sorted by keys to the client?

Carpet4
  • 578
  • 1
  • 8
  • 17

1 Answers1

0

Byte stream is not JSON serializable string, convert the byte stream into a string by decoding it.

results[key] = str(byteIO.getvalue().decode("utf-8",errors='replace'))
harshil9968
  • 3,254
  • 1
  • 16
  • 26