Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cb7364

Browse files
jklymakMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #13405: Fix imshow()ing PIL-opened images.
1 parent 1fc42b3 commit 4cb7364

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lib/matplotlib/image.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,7 @@ def set_data(self, A):
635635
636636
Note that this function does *not* update the normalization used.
637637
"""
638-
# check if data is PIL Image without importing Image
639-
if hasattr(A, 'getpixel'):
640-
if A.mode == 'L':
641-
# greyscale image, but our logic assumes rgba:
642-
self._A = pil_to_array(A.convert('RGBA'))
643-
else:
644-
self._A = pil_to_array(A)
645-
else:
646-
self._A = cbook.safe_masked_invalid(A, copy=True)
638+
self._A = cbook.safe_masked_invalid(A, copy=True)
647639

648640
if (self._A.dtype != np.uint8 and
649641
not np.can_cast(self._A.dtype, float, "same_kind")):

lib/matplotlib/tests/test_image.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
rc_context, rcParams)
1818
from matplotlib.image import (AxesImage, BboxImage, FigureImage,
1919
NonUniformImage, PcolorImage)
20-
from matplotlib.testing.decorators import image_comparison
20+
from matplotlib.testing.decorators import check_figures_equal, image_comparison
2121
from matplotlib.transforms import Bbox, Affine2D, TransformedBbox
2222

2323
import pytest
@@ -105,6 +105,15 @@ def test_image_python_io():
105105
plt.imread(buffer)
106106

107107

108+
@check_figures_equal()
109+
def test_imshow_pil(fig_test, fig_ref):
110+
pytest.importorskip("PIL")
111+
img = plt.imread(os.path.join(os.path.dirname(__file__),
112+
'baseline_images', 'test_image', 'uint16.tif'))
113+
fig_test.subplots().imshow(img)
114+
fig_ref.subplots().imshow(np.asarray(img))
115+
116+
108117
def test_imread_pil_uint16():
109118
pytest.importorskip("PIL")
110119
img = plt.imread(os.path.join(os.path.dirname(__file__),

0 commit comments

Comments
 (0)