Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 02adc55

Browse filesBrowse files
jklymakMeeseeksDev[bot]
authored andcommitted
Backport PR #14326: Correctly apply PNG palette when building ImageBase through Pillow.
1 parent 31ab802 commit 02adc55
Copy full SHA for 02adc55

File tree

Expand file treeCollapse file tree

2 files changed

+19
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+19
-7
lines changed

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,15 @@ def set_data(self, A):
668668
669669
Parameters
670670
----------
671-
A : array-like
671+
A : array-like or `PIL.Image.Image`
672672
"""
673+
try:
674+
from PIL import Image
675+
except ImportError:
676+
pass
677+
else:
678+
if isinstance(A, Image.Image):
679+
A = pil_to_array(A) # Needed e.g. to apply png palette.
673680
self._A = cbook.safe_masked_invalid(A, copy=True)
674681

675682
if (self._A.dtype != np.uint8 and

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+11-6Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from numpy.testing import assert_array_equal
1414

1515
from matplotlib import (
16-
colors, image as mimage, patches, pyplot as plt,
16+
colors, image as mimage, patches, pyplot as plt, style,
1717
rc_context, rcParams)
1818
from matplotlib.cbook import MatplotlibDeprecationWarning
1919
from matplotlib.image import (AxesImage, BboxImage, FigureImage,
@@ -117,11 +117,16 @@ def test_image_python_io():
117117

118118
@check_figures_equal()
119119
def test_imshow_pil(fig_test, fig_ref):
120-
pytest.importorskip("PIL")
121-
img = plt.imread(os.path.join(os.path.dirname(__file__),
122-
'baseline_images', 'test_image', 'uint16.tif'))
123-
fig_test.subplots().imshow(img)
124-
fig_ref.subplots().imshow(np.asarray(img))
120+
style.use("default")
121+
PIL = pytest.importorskip("PIL")
122+
png_path = Path(__file__).parent / "baseline_images/pngsuite/basn3p04.png"
123+
tiff_path = Path(__file__).parent / "baseline_images/test_image/uint16.tif"
124+
axs = fig_test.subplots(2)
125+
axs[0].imshow(PIL.Image.open(png_path))
126+
axs[1].imshow(PIL.Image.open(tiff_path))
127+
axs = fig_ref.subplots(2)
128+
axs[0].imshow(plt.imread(str(png_path)))
129+
axs[1].imshow(plt.imread(tiff_path))
125130

126131

127132
def test_imread_pil_uint16():

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.