Skip to content

Correctly apply PNG palette when building ImageBase through Pillow. #14326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2019
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