Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,15 @@ def set_data(self, A):

Parameters
----------
A : array-like
A : array-like or `PIL.Image.Image`
"""
try:
from PIL import Image
except ImportError:
pass
else:
if isinstance(A, Image.Image):
A = pil_to_array(A) # Needed e.g. to apply png palette.
self._A = cbook.safe_masked_invalid(A, copy=True)

if (self._A.dtype != np.uint8 and
Expand Down
17 changes: 11 additions & 6 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from numpy.testing import assert_array_equal

from matplotlib import (
colors, image as mimage, patches, pyplot as plt,
colors, image as mimage, patches, pyplot as plt, style,
rc_context, rcParams)
from matplotlib.cbook import MatplotlibDeprecationWarning
from matplotlib.image import (AxesImage, BboxImage, FigureImage,
Expand Down Expand Up @@ -117,11 +117,16 @@ def test_image_python_io():

@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))
style.use("default")
PIL = pytest.importorskip("PIL")
png_path = Path(__file__).parent / "baseline_images/pngsuite/basn3p04.png"
tiff_path = Path(__file__).parent / "baseline_images/test_image/uint16.tif"
axs = fig_test.subplots(2)
axs[0].imshow(PIL.Image.open(png_path))
axs[1].imshow(PIL.Image.open(tiff_path))
axs = fig_ref.subplots(2)
axs[0].imshow(plt.imread(str(png_path)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - why is this "str" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because imread() doesn't support Path inputs for png only.

axs[1].imshow(plt.imread(tiff_path))


def test_imread_pil_uint16():
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy