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
34 changes: 34 additions & 0 deletions src/machinevisiontoolbox/base/imageio.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,32 @@ def safe_plt_show(block: bool | float | None = False) -> None:
plt.show(block=bool(block))


def _idisp_key_help(clipboard: bool) -> str:
"""Text listing the keys that :func:`idisp` responds to.

:param clipboard: True if the clipboard keys are available, that is ``pyclip``
is installed
:return: the help text, one key per line
"""
lines = [
"idisp keys:",
" ? show this help",
" q close the window",
]
if clipboard:
lines += [
" c copy the pixel position u,v to the clipboard",
" C append the pixel position to the clipboard",
" v copy the pixel position and value",
" V append the pixel position and value",
" x clear the clipboard",
]
else:
lines.append(" c C v V x clipboard keys, need pyclip: pip install pyclip")
lines.append("Matplotlib: f full screen, g grid, h home, p pan, o zoom, s save")
return "\n".join(lines)


def idisp(
im: np.ndarray,
colororder: str | None = "RGB",
Expand Down Expand Up @@ -402,6 +428,11 @@ def idisp(
Certain keys can be pressed while the image is displayed:

- ``q`` will close the window
- ``?`` will print this list of keys, in the terminal or the notebook output.
The clipboard keys are listed only if ``pyclip`` is installed.

.. versionadded:: 2.5.0
The ``?`` key.

If ``pyclip`` is installed, the following clipboard keys are available:

Expand Down Expand Up @@ -930,6 +961,9 @@ def pixel_text(u: int, v: int) -> str:
return str(tuple(val.tolist()))

def key_press(event: Any) -> None:
if event.key == "?":
print(_idisp_key_help(_pyclip and pyclip is not None))
return
if not _pyclip:
return
if pyclip is None:
Expand Down
69 changes: 69 additions & 0 deletions tests/base/test_idisp_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
depend on the machine the tests run on.
"""

import contextlib
import io
import unittest
from unittest.mock import patch

Expand Down Expand Up @@ -280,6 +282,73 @@ def test_other_keys_and_outside_clicks_do_nothing(self):
self.assertEqual(self.clip.text, "")


class TestKeyHelp(_Agg):
"""The ``?`` key prints the list of keys, which depends on whether pyclip is installed."""

def press(self, key, x=None, y=None):
h = self.show(np.zeros((4, 4), dtype=np.uint8))
canvas = h.axes.figure.canvas
if x is None:
x, y = h.axes.transData.transform((2, 1))
stream = io.StringIO()
with contextlib.redirect_stdout(stream):
canvas.callbacks.process(
"key_press_event", KeyEvent("key_press_event", canvas, key, x, y)
)
return stream.getvalue()

def with_clipboard(self, available):
patches = [
patch.object(imageio, "_pyclip", available),
patch.object(
imageio, "pyclip", _FakeClipboard() if available else None, create=True
),
]
for p in patches:
p.start()
self.addCleanup(p.stop)

def test_help_with_clipboard_keys(self):
self.with_clipboard(True)
text = self.press("?")
for line in (
"? show this help",
"q close the window",
"c copy",
"C append",
"v copy",
"V append",
"x clear",
):
self.assertIn(line, text)
self.assertNotIn("need pyclip", text)

def test_help_without_clipboard_keys(self):
self.with_clipboard(False)
text = self.press("?")
self.assertIn("? show this help", text)
self.assertIn("need pyclip", text)
# the individual clipboard keys are not listed
self.assertNotIn("append the pixel position", text)

def test_help_works_with_the_cursor_outside_the_image(self):
self.with_clipboard(True)
self.assertIn("idisp keys:", self.press("?", -50, -50))

def test_other_keys_print_nothing(self):
self.with_clipboard(True)
self.assertEqual(self.press("z"), "")

def test_help_text_is_concise(self):
for available in (True, False):
lines = imageio._idisp_key_help(available).splitlines()
self.assertLessEqual(len(lines), 12)
self.assertTrue(all(len(line) < 80 for line in lines))

def test_help_lists_matplotlib_keys(self):
self.assertIn("h home", imageio._idisp_key_help(False))


class TestOpenCVWindows(unittest.TestCase):
"""The OpenCV window functions, with cv2 replaced by a recorder."""

Expand Down
Loading