Issue
I am plotting figure as:
plt.imshow(image, cmap='gray', interpolation='none')
plt.imshow(masked_contour, cmap='cool', interpolation='none', alpha=0.7)
plt.show()
The figure shows in greyscale with a blue contour inside it.
Now I want to get this figure as a numpy
array (also not as a masked array). One way can be, save the plot as an image, then read it from there. Is there any better approach?
Solution
fig = plt.figure(figsize=(20, 20)) # this is imp for sizing
# plot
plt.imshow(image, cmap='gray', interpolation='none')
plt.imshow(masked_contour, cmap='cool', interpolation='none', alpha=0.7)
# get image as np.array
canvas = plt.gca().figure.canvas
canvas.draw()
data = np.frombuffer(canvas.tostring_rgb(), dtype=np.uint8)
image = data.reshape(canvas.get_width_height()[::-1] + (3,))
# (Optional) show image
plt.imshow(image)
plt.show()
Answered By - Dr.PB
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.