Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/machinevisiontoolbox/base/imageio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
23 changes: 16 additions & 7 deletions tests/base/test_idisp_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
depend on the machine the tests run on.
"""

import re
import unittest
from unittest.mock import patch

Expand Down Expand Up @@ -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))
Expand All @@ -250,16 +245,30 @@ 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)
im[1, 2] = (10, 20, 30)
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")
Expand Down
Loading