diff --git a/src/machinevisiontoolbox/ImageIO.py b/src/machinevisiontoolbox/ImageIO.py index dea30a24..e99e66a0 100644 --- a/src/machinevisiontoolbox/ImageIO.py +++ b/src/machinevisiontoolbox/ImageIO.py @@ -398,7 +398,8 @@ def showpixels( if isinstance(cmap, str): cmap = plt.get_cmap(cmap) if badcolor is not None: - cmap.set_bad(color=badcolor) + # with_extremes returns a copy, so the caller's colormap is not changed + cmap = cmap.with_extremes(bad=badcolor) ax.imshow(image, cmap=cmap, zorder=1) # label the cells with the pixel values diff --git a/src/machinevisiontoolbox/base/imageio.py b/src/machinevisiontoolbox/base/imageio.py index 5ba69db3..c69115c3 100644 --- a/src/machinevisiontoolbox/base/imageio.py +++ b/src/machinevisiontoolbox/base/imageio.py @@ -4,7 +4,6 @@ from __future__ import annotations -import copy import glob import urllib.error import urllib.request @@ -778,13 +777,18 @@ def _idisp_matplotlib( # # only works for greyscale image if im.ndim == 2: - cmap = copy.copy(cmap) - if undercolor is not None: - cmap.set_under(color=undercolor) - if overcolor is not None: - cmap.set_over(color=overcolor) - if badcolor is not None: - cmap.set_bad(color=badcolor) + # with_extremes returns a copy, so the caller's colormap is not changed + extremes = { + key: color + for key, color in ( + ("under", undercolor), + ("over", overcolor), + ("bad", badcolor), + ) + if color is not None + } + if extremes: + cmap = cmap.with_extremes(**extremes) # elif im.ndim == 3: # if badcolor is not None: # cmap.set_bad(color=badcolor) diff --git a/tests/base/test_io.py b/tests/base/test_io.py index 62d49675..8bfaac64 100644 --- a/tests/base/test_io.py +++ b/tests/base/test_io.py @@ -1031,6 +1031,33 @@ def test_idisp_badcolor_option(self): result = idisp(im, matplotlib=False, badcolor="red") self.assertIsNone(result) + def test_idisp_extreme_colors_without_a_deprecation_warning(self): + """Colormap.set_under, set_over and set_bad are pending deprecation""" + import warnings + + import matplotlib + import matplotlib.pyplot as plt + + from machinevisiontoolbox.base.imageio import idisp + + matplotlib.use("Agg") + im = np.random.randint(100, 200, (30, 30), dtype=np.uint8) + with warnings.catch_warnings(): + warnings.simplefilter("error", PendingDeprecationWarning) + idisp( + im, + undercolor="blue", + overcolor="yellow", + badcolor="red", + vrange=(120, 180), + block=False, + ) + cmap = plt.gca().images[0].get_cmap() + np.testing.assert_allclose(cmap.get_under(), (0.0, 0.0, 1.0, 1.0)) + np.testing.assert_allclose(cmap.get_over(), (1.0, 1.0, 0.0, 1.0)) + np.testing.assert_allclose(cmap.get_bad(), (1.0, 0.0, 0.0, 1.0)) + plt.close("all") + def test_idisp_undercolor_overcolor(self): """Test idisp with undercolor and overcolor""" im = np.random.randint(100, 200, (30, 30), dtype=np.uint8) diff --git a/tests/test_image_io_extra.py b/tests/test_image_io_extra.py index fad763e4..eb58b367 100644 --- a/tests/test_image_io_extra.py +++ b/tests/test_image_io_extra.py @@ -168,6 +168,24 @@ def test_colormap_by_name_and_bad_color(self): ax = im.showpixels(cmap="gray", badcolor="red") assert ax.images[0].get_cmap().name == "gray" + def test_bad_color_without_a_deprecation_warning(self): + # Colormap.set_bad is pending deprecation in Matplotlib + import warnings + + im = Image(np.array([[1.0, np.nan]])) + with warnings.catch_warnings(): + warnings.simplefilter("error", PendingDeprecationWarning) + ax = im.showpixels(cmap="gray", badcolor="red") + nt.assert_allclose(ax.images[0].get_cmap().get_bad(), (1.0, 0.0, 0.0, 1.0)) + + def test_bad_color_leaves_the_callers_colormap_alone(self): + from matplotlib.colors import LinearSegmentedColormap + + cmap = LinearSegmentedColormap.from_list("mine", ["#000000", "#ffffff"]) + before = np.array(cmap.get_bad()) + Image(np.array([[1.0, np.nan]])).showpixels(cmap=cmap, badcolor="red") + nt.assert_allclose(cmap.get_bad(), before) + def test_default_colormap(self): ax = Image(np.zeros((2, 2), dtype=np.uint8)).showpixels() assert ax.images[0].get_cmap().name == "midnight"