From 4cb7364b7968a1f5992b428081d42d5814cd4644 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 11 Feb 2019 16:52:34 -0800 Subject: [PATCH 1/2] Backport PR #13405: Fix imshow()ing PIL-opened images. --- lib/matplotlib/image.py | 10 +--------- lib/matplotlib/tests/test_image.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 3bdcccf48821..e03778ca9f87 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -635,15 +635,7 @@ def set_data(self, A): Note that this function does *not* update the normalization used. """ - # check if data is PIL Image without importing Image - if hasattr(A, 'getpixel'): - if A.mode == 'L': - # greyscale image, but our logic assumes rgba: - self._A = pil_to_array(A.convert('RGBA')) - else: - self._A = pil_to_array(A) - else: - self._A = cbook.safe_masked_invalid(A, copy=True) + self._A = cbook.safe_masked_invalid(A, copy=True) if (self._A.dtype != np.uint8 and not np.can_cast(self._A.dtype, float, "same_kind")): diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index c8a23347f65e..2105edbb2775 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -17,7 +17,7 @@ rc_context, rcParams) from matplotlib.image import (AxesImage, BboxImage, FigureImage, NonUniformImage, PcolorImage) -from matplotlib.testing.decorators import image_comparison +from matplotlib.testing.decorators import check_figures_equal, image_comparison from matplotlib.transforms import Bbox, Affine2D, TransformedBbox import pytest @@ -105,6 +105,15 @@ def test_image_python_io(): plt.imread(buffer) +@check_figures_equal() +def test_imshow_pil(fig_test, fig_ref): + pytest.importorskip("PIL") + img = plt.imread(os.path.join(os.path.dirname(__file__), + 'baseline_images', 'test_image', 'uint16.tif')) + fig_test.subplots().imshow(img) + fig_ref.subplots().imshow(np.asarray(img)) + + def test_imread_pil_uint16(): pytest.importorskip("PIL") img = plt.imread(os.path.join(os.path.dirname(__file__), From f6eac94ce70a60e8f5a1d3d015b6fa673436929f Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 11 Feb 2019 22:31:34 -0500 Subject: [PATCH 2/2] TST: remove test that relies of tools not backported to v2.2.x --- lib/matplotlib/tests/test_image.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 2105edbb2775..c8a23347f65e 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -17,7 +17,7 @@ rc_context, rcParams) from matplotlib.image import (AxesImage, BboxImage, FigureImage, NonUniformImage, PcolorImage) -from matplotlib.testing.decorators import check_figures_equal, image_comparison +from matplotlib.testing.decorators import image_comparison from matplotlib.transforms import Bbox, Affine2D, TransformedBbox import pytest @@ -105,15 +105,6 @@ def test_image_python_io(): plt.imread(buffer) -@check_figures_equal() -def test_imshow_pil(fig_test, fig_ref): - pytest.importorskip("PIL") - img = plt.imread(os.path.join(os.path.dirname(__file__), - 'baseline_images', 'test_image', 'uint16.tif')) - fig_test.subplots().imshow(img) - fig_ref.subplots().imshow(np.asarray(img)) - - def test_imread_pil_uint16(): pytest.importorskip("PIL") img = plt.imread(os.path.join(os.path.dirname(__file__),