forked from matplotlib/napari-matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
29 lines (22 loc) · 857 Bytes
/
Copy pathhelpers.py
File metadata and controls
29 lines (22 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from io import BytesIO
import numpy as np
import numpy.typing as npt
import pytest
from matplotlib.figure import Figure
def fig_to_array(fig: Figure) -> npt.NDArray[np.uint8]:
"""
Convert a figure to an RGB array.
"""
with BytesIO() as io_buf:
fig.savefig(io_buf, format="raw")
io_buf.seek(0)
img_arr: npt.NDArray[np.uint8] = np.reshape(
np.frombuffer(io_buf.getvalue(), dtype=np.uint8),
shape=(int(fig.bbox.bounds[3]), int(fig.bbox.bounds[2]), -1),
)
return img_arr
def assert_figures_equal(fig1: Figure, fig2: Figure) -> None:
np.testing.assert_equal(fig_to_array(fig1), fig_to_array(fig2))
def assert_figures_not_equal(fig1: Figure, fig2: Figure) -> None:
with pytest.raises(AssertionError, match="Arrays are not equal"):
assert_figures_equal(fig1, fig2)