简体   繁体   中英

How do get the raw plot image data from matplotlib? Instead of saving to file, or displaying

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM