| 1 |
import io |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
import PIL.Image |
| 5 |
|
| 6 |
|
| 7 |
def pyplot_to_numpy(): |
| 8 |
"""Convert pyplot state to numpy array. |
| 9 |
|
| 10 |
Parameters |
| 11 |
---------- |
| 12 |
|
| 13 |
Returns |
| 14 |
------- |
| 15 |
arr: numpy.ndarray |
| 16 |
Plotted image. |
| 17 |
|
| 18 |
""" |
| 19 |
import matplotlib.pyplot as plt |
| 20 |
|
| 21 |
f = io.BytesIO() |
| 22 |
plt.savefig( |
| 23 |
f, |
| 24 |
bbox_inches="tight", |
| 25 |
transparent="True", |
| 26 |
pad_inches=0, |
| 27 |
format="jpeg", |
| 28 |
) |
| 29 |
plt.close() |
| 30 |
f.seek(0) |
| 31 |
arr = np.asarray(PIL.Image.open(f)) |
| 32 |
return arr |
| 33 |
|
| 34 |
|
| 35 |
def pyplot_imshow(image): |
| 36 |
import matplotlib.pyplot as plt |
| 37 |
|
| 38 |
plt.cla() |
| 39 |
plt.imshow(image) |
| 40 |
plt.tight_layout() |
| 41 |
plt.show() |