0

I know how to extract the image if it was rendered in its own subplot, like desribed here:

Matplotlib figure to image as a numpy array

However, I have a slightly different code, which looks like the following:

_, ax = plt.subplots(1, figsize=(20,20))


height, width = image.shape[:2]
ax.set_ylim(height + 10, -10)
ax.set_xlim(-10, width + 10)
ax.axis('off')
ax.set_title(title)
ax.text(x,y,"blabla")
#...some more things happen with ax here

some_image = 255*np.random.rand(20,20)
some_image = some_image.astype(np.uint8)

#Somethings happen with the image here
#....


ax.imshow(some_image.astype(np.uint8))

Is it possible to get the shown ax object as a numpy array for furhter processing without rewriting the code?

Kev1n91
  • 3,553
  • 8
  • 46
  • 96
  • 1
    The numpy array that is shown is still `some_image.astype(np.uint8)`, so why not simply use it? – ImportanceOfBeingErnest Dec 11 '17 at 10:22
  • More things as mentioned in the example code are done to ax, like adding text and rectangles, - which will miss in some_image – Kev1n91 Dec 11 '17 at 10:32
  • What is the problem with using the solution from the linked question? – ImportanceOfBeingErnest Dec 11 '17 at 10:41
  • Would [this solution](https://stackoverflow.com/questions/43096972/how-can-i-render-a-matplotlib-axes-object-to-an-image-as-a-numpy-array?rq=1) suit you more? – ImportanceOfBeingErnest Dec 11 '17 at 10:46
  • @ImportanceOfBeingErnest Thank you I will take a look at it. The shown solution would lead me to rewrite the code (which is not from me), so I wanted to know if there was a more direct way. Thank you again – Kev1n91 Dec 11 '17 at 10:49
  • 1
    I don't see why you would rewrite any code. The solution in [this question](https://stackoverflow.com/questions/35355930/matplotlib-figure-to-image-as-a-numpy-array) you link to can be directly applied to your case, by just adding the lines `ax.figure.canvas.draw(); image = np.fromstring(ax.figure.canvas.tostring_rgb(), dtype='uint8')`. Also the solution in the question I link to can be appended after the code. The main difference is that the former would give you the complete canvas, while the latter would give you only the axes `ax`. – ImportanceOfBeingErnest Dec 11 '17 at 10:54
  • I actually added this to my code, however it is serialized in a 1dim array - is it possible to know the original dimensions to which it could be reshaped? – Kev1n91 Dec 11 '17 at 14:46
  • The size of "image" is (7680000,1) and I cannot figure out what the original size might be – Kev1n91 Dec 11 '17 at 14:56
  • Did you actually read [the comment below the answer](https://stackoverflow.com/questions/35355930/matplotlib-figure-to-image-as-a-numpy-array#comment72722681_35362787)? – ImportanceOfBeingErnest Dec 11 '17 at 15:05
  • I guess I have to add more than the mentioned code in your previous comment when this occurs, like canvas, fig etc. ? – Kev1n91 Dec 11 '17 at 15:15
  • `ax`, `ax.figure`, `ax.figure.canvas`, what else do you need? – ImportanceOfBeingErnest Dec 11 '17 at 15:18

0 Answers0