Skip to content

Cleanup following using Pillow as universal image reader #15571

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 2 commits into from
Oct 30, 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
5 changes: 2 additions & 3 deletions INSTALL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ To run the test suite:
* extract the :file:`lib/matplotlib/tests` or :file:`lib/mpl_toolkits/tests`
directories from the source distribution;
* install test dependencies: `pytest <https://pypi.org/project/pytest>`_,
Pillow, MiKTeX, GhostScript, ffmpeg, avconv, ImageMagick, and `Inkscape
MiKTeX, GhostScript, ffmpeg, avconv, ImageMagick, and `Inkscape
<https://inkscape.org/>`_;
* run ``python -mpytest``.

Expand Down Expand Up @@ -126,6 +126,7 @@ Matplotlib requires the following dependencies:
* `cycler <http://matplotlib.org/cycler/>`_ (>= 0.10.0)
* `dateutil <https://pypi.org/project/python-dateutil>`_ (>= 2.1)
* `kiwisolver <https://github.com/nucleic/kiwi>`_ (>= 1.0.0)
* `Pillow <https://pillow.readthedocs.io/en/latest/>`_ (>= 6.2)
* `pyparsing <https://pyparsing.wikispaces.com/>`_

Optionally, you can also install a number of packages to enable better user
Expand Down Expand Up @@ -160,8 +161,6 @@ etc., you can install the following:
<https://libav.org/avconv.html>`_: for saving movies;
* `ImageMagick <https://www.imagemagick.org/script/index.php>`_: for saving
animated gifs;
* `Pillow <https://pillow.readthedocs.io/en/latest/>`_ (>= 3.4): for a larger
selection of image file formats: JPEG, BMP, and TIFF image files;
* `LaTeX <https://miktex.org/>`_ and `GhostScript (>=9.0)
<https://ghostscript.com/download/>`_ : for rendering text with LaTeX.

Expand Down
4 changes: 1 addition & 3 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2143,9 +2143,7 @@ def savefig(self, fname, *, transparent=None, **kwargs):

pil_kwargs : dict, optional
Additional keyword arguments that are passed to `PIL.Image.save`
when saving the figure. Only applicable for formats that are saved
using Pillow, i.e. JPEG, TIFF, and (if the keyword is set to a
non-None value) PNG.
when saving the figure.
"""

kwargs.setdefault('dpi', rcParams['savefig.dpi'])
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,8 +1629,9 @@ def thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear',
Parameters
----------
infile : str or file-like
The image file -- must be PNG, or Pillow-readable if you have Pillow_
installed.
The image file. Matplotlib relies on Pillow_ for image reading, and
thus supports a wide range of file formats, including PNG, JPG, TIFF
and others.

.. _Pillow: http://python-pillow.org/

Expand Down
8 changes: 3 additions & 5 deletions lib/matplotlib/tests/test_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import numpy as np
from numpy.testing import assert_array_almost_equal
from PIL import Image, TiffTags
import pytest


from matplotlib import (
collections, path, pyplot as plt, transforms as mtransforms, rcParams)
from matplotlib.image import imread
Expand Down Expand Up @@ -217,7 +219,6 @@ def test_chunksize():

@pytest.mark.backend('Agg')
def test_jpeg_dpi():
Image = pytest.importorskip("PIL.Image")
# Check that dpi is set correctly in jpg files.
plt.plot([0, 1, 2], [0, 1, 0])
buf = io.BytesIO()
Expand All @@ -227,7 +228,6 @@ def test_jpeg_dpi():


def test_pil_kwargs_png():
Image = pytest.importorskip("PIL.Image")
from PIL.PngImagePlugin import PngInfo
buf = io.BytesIO()
pnginfo = PngInfo()
Expand All @@ -238,11 +238,9 @@ def test_pil_kwargs_png():


def test_pil_kwargs_tiff():
Image = pytest.importorskip("PIL.Image")
from PIL.TiffTags import TAGS_V2 as TAGS
buf = io.BytesIO()
pil_kwargs = {"description": "test image"}
plt.figure().savefig(buf, format="tiff", pil_kwargs=pil_kwargs)
im = Image.open(buf)
tags = {TAGS[k].name: v for k, v in im.tag_v2.items()}
tags = {TiffTags.TAGS_V2[k].name: v for k, v in im.tag_v2.items()}
assert tags["ImageDescription"] == "test image"
2 changes: 0 additions & 2 deletions lib/matplotlib/tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ def isAvailable(cls):
# matplotlib.testing.image_comparison
@pytest.mark.parametrize('writer, output', WRITER_OUTPUT)
def test_save_animation_smoketest(tmpdir, writer, output):
if writer == 'pillow':
pytest.importorskip("PIL")
try:
# for ImageMagick the rcparams must be patched to account for
# 'convert' being a built in MS tool, not the imagemagick
Expand Down
23 changes: 5 additions & 18 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np
from numpy import ma
from numpy.testing import assert_array_equal
from PIL import Image

from matplotlib import (
colors, image as mimage, patches, pyplot as plt, style, rcParams)
Expand Down Expand Up @@ -219,31 +220,24 @@ def test_imshow_zoom(fig_test, fig_ref):
@check_figures_equal()
def test_imshow_pil(fig_test, fig_ref):
style.use("default")
PIL = pytest.importorskip("PIL")
# Pillow<=6.0 fails to open pathlib.Paths on Windows (pillow#3823), and
# Matplotlib's builtin png opener doesn't handle them either.
png_path = str(
Path(__file__).parent / "baseline_images/pngsuite/basn3p04.png")
tiff_path = str(
Path(__file__).parent / "baseline_images/test_image/uint16.tif")
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[0].imshow(Image.open(png_path))
axs[1].imshow(Image.open(tiff_path))
axs = fig_ref.subplots(2)
axs[0].imshow(plt.imread(png_path))
axs[1].imshow(plt.imread(tiff_path))


def test_imread_pil_uint16():
pytest.importorskip("PIL")
img = plt.imread(os.path.join(os.path.dirname(__file__),
'baseline_images', 'test_image', 'uint16.tif'))
assert img.dtype == np.uint16
assert np.sum(img) == 134184960


def test_imread_fspath():
pytest.importorskip("PIL")
img = plt.imread(
Path(__file__).parent / 'baseline_images/test_image/uint16.tif')
assert img.dtype == np.uint16
Expand All @@ -252,8 +246,6 @@ def test_imread_fspath():

@pytest.mark.parametrize("fmt", ["png", "jpg", "jpeg", "tiff"])
def test_imsave(fmt):
if fmt in ["jpg", "jpeg", "tiff"]:
pytest.importorskip("PIL")
has_alpha = fmt not in ["jpg", "jpeg"]

# The goal here is that the user can specify an output logical DPI
Expand Down Expand Up @@ -318,7 +310,6 @@ def test_imsave_color_alpha():


def test_imsave_pil_kwargs_png():
Image = pytest.importorskip("PIL.Image")
from PIL.PngImagePlugin import PngInfo
buf = io.BytesIO()
pnginfo = PngInfo()
Expand All @@ -330,7 +321,6 @@ def test_imsave_pil_kwargs_png():


def test_imsave_pil_kwargs_tiff():
Image = pytest.importorskip("PIL.Image")
from PIL.TiffTags import TAGS_V2 as TAGS
buf = io.BytesIO()
pil_kwargs = {"description": "test image"}
Expand Down Expand Up @@ -668,7 +658,6 @@ def test_nonuniformimage_setnorm():


def test_jpeg_2d():
Image = pytest.importorskip('PIL.Image')
# smoke test that mode-L pillow images work.
imd = np.ones((10, 10), dtype='uint8')
for i in range(10):
Expand All @@ -680,8 +669,6 @@ def test_jpeg_2d():


def test_jpeg_alpha():
Image = pytest.importorskip('PIL.Image')

plt.figure(figsize=(1, 1), dpi=300)
# Create an image that is all black, with a gradient from 0-1 in
# the alpha channel from left to right.
Expand Down