Skip to content

Commit 8d64233

Browse files
authored
Merge pull request #94 from petercorke/fix/idisp-reuse-no-title
2 parents c34ff15 + a05b13b commit 8d64233

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎src/machinevisiontoolbox/base/imageio.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,15 @@ def key_press(event: Any) -> None:
882882
## display using OpenCV
883883
global __last_window_number
884884

885-
if not reuse and title is None:
886-
# create a unique window name for each call
887-
title = "idisp." + str(__last_window_number)
888-
__last_window_number += 1
885+
if title is None:
886+
if reuse:
887+
# fixed name (no counter) so repeated calls in an
888+
# animation loop share the same window
889+
title = "idisp"
890+
else:
891+
# create a unique window name for each call
892+
title = "idisp." + str(__last_window_number)
893+
__last_window_number += 1
889894

890895
# At this point title is guaranteed to be a string
891896
assert title is not None

‎tests/base/test_io.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,20 @@ def test_idisp_uint16_image(self):
369369
result = idisp(im, matplotlib=False)
370370
self.assertIsNone(result)
371371

372+
def test_idisp_reuse_no_title(self):
373+
"""reuse=True with no title must not crash -- regression test for
374+
the OpenCV path's title fallback (previously asserted title is not
375+
None even though reuse=True skipped generating one)"""
376+
from machinevisiontoolbox.base.imageio import cv_destroy_window, idisp
377+
378+
im = np.random.randint(0, 256, (30, 30), dtype=np.uint8)
379+
380+
try:
381+
idisp(im, matplotlib=False, reuse=True)
382+
idisp(im, matplotlib=False, reuse=True)
383+
finally:
384+
cv_destroy_window("idisp", block=False)
385+
372386
def test_idisp_float_image(self):
373387
"""Test idisp with float image"""
374388
im = np.random.rand(30, 30).astype(np.float32)

0 commit comments

Comments
 (0)