diff --git a/src/machinevisiontoolbox/base/imageio.py b/src/machinevisiontoolbox/base/imageio.py index 07b720b3..d1b45cab 100644 --- a/src/machinevisiontoolbox/base/imageio.py +++ b/src/machinevisiontoolbox/base/imageio.py @@ -429,6 +429,11 @@ def idisp( U1,V1\nU2,V2 - The clipboard keys require that ``pyclip`` is installed. + .. versionchanged:: 2.5.0 + The ``v`` and ``V`` keys copy plain numbers, for example ``2,1,(10, 20, 30)``. + On NumPy 2 they used to copy ``2,1,(np.uint8(10), np.uint8(20), np.uint8(30))``, + and raised an error for a monochrome image. + :references: - |RVC3|, Section 10.1. @@ -915,6 +920,15 @@ def format_coord(u: float, v: float) -> str: except IndexError: return "" + def pixel_text(u: int, v: int) -> str: + # the value of pixel (u, v) as plain Python numbers: "6" for a monochrome + # image, "(10, 20, 30)" for a color image. NumPy 2 would show a scalar + # as "np.uint8(10)", which is no use pasted into a script or a spreadsheet. + val = im[v, u, ...] + if val.ndim == 0: + return str(val.item()) + return str(tuple(val.tolist())) + def key_press(event: Any) -> None: if not _pyclip: return @@ -939,14 +953,12 @@ def key_press(event: Any) -> None: elif event.key == "x": pyclip.copy("") elif event.key == "v": - val = im[v, u, ...] - pyclip.copy(f"{u},{v},{tuple(val)}") + pyclip.copy(f"{u},{v},{pixel_text(u, v)}") elif event.key == "V": - val = im[v, u, ...] prev = pyclip.paste() if isinstance(prev, bytes): prev = prev.decode() - pyclip.copy(f"{prev}\n{u},{v},{tuple(val)}") + pyclip.copy(f"{prev}\n{u},{v},{pixel_text(u, v)}") fig.canvas.mpl_connect("key_press_event", key_press) diff --git a/tests/base/test_idisp_options.py b/tests/base/test_idisp_options.py index 41dc5aca..d432f900 100644 --- a/tests/base/test_idisp_options.py +++ b/tests/base/test_idisp_options.py @@ -9,7 +9,6 @@ depend on the machine the tests run on. """ -import re import unittest from unittest.mock import patch @@ -225,10 +224,6 @@ def setUp(self): self.h = self.show(self.im) self.ax = self.h.axes - def copied(self): - # NumPy 2 prints a scalar as np.uint8(10); accept that as well as plain 10 - return re.sub(r"np\.\w+\((\d+)\)", r"\1", self.clip.text) - def press(self, key, u=2, v=1): canvas = self.ax.figure.canvas x, y = self.ax.transData.transform((u, v)) @@ -250,6 +245,20 @@ def test_x_clears_the_clipboard(self): self.press("x") self.assertEqual(self.clip.text, "") + def test_v_and_V_with_a_monochrome_image(self): + # the value is a plain number + self.press("v", 2, 1) + self.assertEqual(self.clip.text, "2,1,6") + self.press("V", 3, 3) + self.assertEqual(self.clip.text, "2,1,6\n3,3,15") + + def test_v_with_a_float_image(self): + plt.close("all") + im = np.full((4, 4), 0.25, dtype=np.float32) + self.ax = self.show(im).axes + self.press("v", 2, 1) + self.assertEqual(self.clip.text, "2,1,0.25") + def test_v_and_V_with_a_color_image(self): plt.close("all") im = np.zeros((4, 4, 3), dtype=np.uint8) @@ -257,9 +266,9 @@ def test_v_and_V_with_a_color_image(self): im[3, 3] = (1, 2, 3) self.ax = self.show(im).axes self.press("v", 2, 1) - self.assertEqual(self.copied(), "2,1,(10, 20, 30)") + self.assertEqual(self.clip.text, "2,1,(10, 20, 30)") self.press("V", 3, 3) - self.assertEqual(self.copied(), "2,1,(10, 20, 30)\n3,3,(1, 2, 3)") + self.assertEqual(self.clip.text, "2,1,(10, 20, 30)\n3,3,(1, 2, 3)") def test_other_keys_and_outside_clicks_do_nothing(self): self.press("z")