Last active
August 9, 2024 10:37
-
-
Save orm011/c674f55566fd83609ba6c41699acb728 to your computer and use it in GitHub Desktop.
pyplot figure to numpy array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plt2arr(fig, draw=True): | |
""" | |
need to draw if figure is not drawn yet | |
""" | |
if draw: | |
fig.canvas.draw() | |
rgba_buf = fig.canvas.buffer_rgba() | |
(w,h) = fig.canvas.get_width_height() | |
rgba_arr = np.frombuffer(rgba_buf, dtype=np.uint8).reshape((h,w,4)) | |
return rgba_arr | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment