1

I want to be able to send the raw image data somewhere else without ever saving to a file or displaying the plot. Thank you for the help!

EDIT

Bytes are what I am interested in. I am trying to send the byte image to a web client site and have it be displayed from js/html

Hairy
  • 393
  • 1
  • 10
  • 2
    I'm not sure if they actually answer your question, but [this question](https://stackoverflow.com/questions/31393769/getting-an-rgba-array-from-a-matplotlib-image) and [this question](https://stackoverflow.com/questions/12144877/get-binary-image-data-from-a-matplotlib-canvas) look relevant. – jirassimok Nov 14 '19 at 05:36
  • 1
    How raw are we talking? Likely you'll want to save to an in memory file (BytesIO). – Mad Physicist Nov 14 '19 at 06:02
  • 1
    Could you add a plain python tag please? – Mad Physicist Nov 14 '19 at 06:04
  • Yeah bytes are what I am interested in. I am trying to send the byte image to a client web site and have it be displayed from js/html. – Hairy Nov 16 '19 at 18:43

1 Answers1

1

You can try using canvas as follows:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, figsize=(4, 4), dpi=300)
ax.plot([1, 3, 5, 8, 4, 2])

fig.canvas.draw()
temp_canvas = fig.canvas
plt.close()

at this point, temp_canvas contains the "raw" matplotlib plot. You can treat it as a raw image and use it within other libraries such as PIL, for example you can plot it:

import PIL
pil_image = PIL.Image.frombytes('RGB', temp_canvas.get_width_height(),  temp_canvas.tostring_rgb())
plt.imshow(pil_image)

output image

Andrea
  • 2,932
  • 11
  • 23