I have the following code
import scipy.misc
import matplotlib.pyplot as plt
a = plt.imshow(scipy.misc.lena())
and what I'm hoping to achieve is get the data on lena by accessing a
or it's children.
The reason is I'll be accessing the image as plt.gcf()
or plt.gca()
a
should be a matplotlib.image.AxesImage
instance, in which case you can use
a.get_array()
and
a.set_array(data)
The array is stored as a masked array
.
Example
There's an official example available at http://matplotlib.org/examples/animation/dynamic_image.html .
Direct access
You can also use
a._A
to access the array data directly, though I imagine that the getters and setters are the preferred method.
### get image from the plot ###
plt.figure()
...
plt.imshow(image)
# remove white padding
plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
plt.axis('off')
plt.axis('image')
# redraw the canvas
fig = plt.gcf()
fig.canvas.draw()
# convert canvas to image using numpy
img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
# opencv format
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
plt.close()
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.