Skip to content

Commit 3db30be

Browse files
committed
Switch to using pillow for png as well.
The version check in _has_pil (pillow>=3.4) was dropped as the oldest Pillow that supports Py3.6 (the oldest Python supported by Matplotlib) is 4.0 anyways. Some complications arise from an unfortunate design decision that imread() returns pngs in 0-1 float dtype but others formats in integer dtype (via Pillow), and from Pillow having not-so-great support for 16-bit images (e.g. Pillow#3041). This makes reading of 16-bit RGB(A) PNGs (which are exposed as 0-1 floats by imread()) slightly less accurate, because Pillow coarsens them to 8-bit first (by truncation); hence the slight change in the pngsuite.png baseline image. (Note that the *renderers* only have 8-bit precision anyways (unless using mplcairo with cairo master...), but the discrepancy comes from differences in rounding between Pillow and Matplotlib).
1 parent 8d17939 commit 3db30be

24 files changed

+237
-1029
lines changed

.appveyor.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ install:
6060
# if conda-forge gets a new pyqt, it might be nice to install it as well to have more backends
6161
# https://github.com/conda-forge/conda-forge.github.io/issues/157#issuecomment-223536381
6262
- conda create -q -n test-environment python=%PYTHON_VERSION%
63-
freetype=2.6 "libpng>=1.6.21,<1.7" zlib=1.2 tk=8.5
63+
freetype=2.6 zlib=1.2 tk=8.5
6464
pip setuptools numpy sphinx tornado
6565
- activate test-environment
6666
- echo %PYTHON_VERSION% %TARGET_ARCH%
@@ -81,11 +81,9 @@ test_script:
8181
# Now build the thing..
8282
- set LINK=/LIBPATH:%cd%\lib
8383
- pip install -ve .
84-
# these should show no z, png, or freetype dll...
84+
# these should show no z or freetype dll...
8585
- set "DUMPBIN=%VS140COMNTOOLS%\..\..\VC\bin\dumpbin.exe"
8686
- 'if x%MPLSTATICBUILD% == xTrue "%DUMPBIN%" /DEPENDENTS lib\matplotlib\ft2font*.pyd | findstr freetype.*.dll && exit /b 1 || exit /b 0'
87-
- 'if x%MPLSTATICBUILD% == xTrue "%DUMPBIN%" /DEPENDENTS lib\matplotlib\_png*.pyd | findstr z.*.dll && exit /b 1 || exit /b 0'
88-
- 'if x%MPLSTATICBUILD% == xTrue "%DUMPBIN%" /DEPENDENTS lib\matplotlib\_png*.pyd | findstr png.*.dll && exit /b 1 || exit /b 0'
8987

9088
# this are optional dependencies so that we don't skip so many tests...
9189
- if x%TEST_ALL% == xyes conda install -q ffmpeg inkscape miktex pillow

INSTALL.rst

+13-15
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ toolchain is prefixed. This may be used for cross compiling. ::
9999
export PKG_CONFIG=x86_64-pc-linux-gnu-pkg-config
100100

101101
Once you have satisfied the requirements detailed below (mainly
102-
Python, NumPy, libpng and FreeType), you can build Matplotlib.
102+
Python, NumPy, and FreeType), you can build Matplotlib.
103103
::
104104

105105
cd matplotlib
@@ -121,7 +121,6 @@ Matplotlib requires the following dependencies:
121121

122122
* `Python <https://www.python.org/downloads/>`_ (>= 3.6)
123123
* `FreeType <https://www.freetype.org/>`_ (>= 2.3)
124-
* `libpng <http://www.libpng.org>`_ (>= 1.2)
125124
* `NumPy <http://www.numpy.org>`_ (>= 1.11)
126125
* `setuptools <https://setuptools.readthedocs.io/en/latest/>`_
127126
* `cycler <http://matplotlib.org/cycler/>`_ (>= 0.10.0)
@@ -177,8 +176,8 @@ etc., you can install the following:
177176
.. _pkg-config: https://www.freedesktop.org/wiki/Software/pkg-config/
178177

179178
If not using pkg-config (in particular on Windows), you may need to set the
180-
include path (to the FreeType, libpng, and zlib headers) and link path (to
181-
the FreeType, libpng, and zlib libraries) explicitly, if they are not in
179+
include path (to the FreeType and zlib headers) and link path (to
180+
the FreeType and zlib libraries) explicitly, if they are not in
182181
standard locations. This can be done using standard environment variables
183182
-- on Linux and OSX:
184183

@@ -195,8 +194,8 @@ etc., you can install the following:
195194
set LINK=/LIBPATH:C:\directory\containing\freetype.lib ...
196195
197196
where ``...`` means "also give, in the same format, the directories
198-
containing ``png.h`` and ``zlib.h`` for the include path, and for
199-
``libpng.so``/``png.lib`` and ``libz.so``/``z.lib`` for the link path."
197+
containing ``zlib.h`` for the include path, and for
198+
``libz.so``/``z.lib`` for the link path."
200199

201200
.. note::
202201

@@ -237,20 +236,20 @@ Building on macOS
237236
-----------------
238237

239238
The build situation on macOS is complicated by the various places one
240-
can get the libpng and FreeType requirements (MacPorts, Fink,
239+
can get FreeType (MacPorts, Fink,
241240
/usr/X11R6), the different architectures (e.g., x86, ppc, universal), and
242241
the different macOS versions (e.g., 10.4 and 10.5). We recommend that you build
243242
the way we do for the macOS release: get the source from the tarball or the
244243
git repository and install the required dependencies through a third-party
245244
package manager. Two widely used package managers are Homebrew, and MacPorts.
246-
The following example illustrates how to install libpng and FreeType using
245+
The following example illustrates how to install FreeType using
247246
``brew``::
248247

249-
brew install libpng freetype pkg-config
248+
brew install freetype pkg-config
250249

251250
If you are using MacPorts, execute the following instead::
252251

253-
port install libpng freetype pkgconfig
252+
port install freetype pkgconfig
254253

255254
After installing the above requirements, install Matplotlib from source by
256255
executing::
@@ -274,7 +273,7 @@ https://packaging.python.org/guides/packaging-binary-extensions/#setting-up-a-bu
274273
for how to set up a build environment.
275274

276275
Since there is no canonical Windows package manager, the methods for building
277-
FreeType, zlib, and libpng from source code are documented as a build script
276+
FreeType and zlib from source code are documented as a build script
278277
at `matplotlib-winbuild <https://github.com/jbmohler/matplotlib-winbuild>`_.
279278

280279
There are a few possibilities to build Matplotlib on Windows:
@@ -290,17 +289,16 @@ Wheel builds using conda packages
290289
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
291290

292291
This is a wheel build, but we use conda packages to get all the requirements.
293-
The binary requirements (png, FreeType,...) are statically linked and therefore
294-
not needed during the wheel install.
292+
FreeType is statically linked and therefore not needed during the wheel install.
295293

296294
Set up the conda environment. Note, if you want a qt backend, add ``pyqt`` to
297295
the list of conda packages.
298296

299297
::
300298

301-
conda create -n "matplotlib_build" python=3.7 numpy python-dateutil pyparsing tornado cycler tk libpng zlib freetype
299+
conda create -n "matplotlib_build" python=3.7 numpy python-dateutil pyparsing tornado cycler tk zlib freetype
302300
conda activate matplotlib_build
303-
# force the build against static libpng and zlib libraries
301+
# force the build against static zlib libraries
304302
set MPLSTATICBUILD=True
305303
python setup.py bdist_wheel
306304

ci/azure-pipelines-steps.yml

-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ steps:
2121
displayName: 'Use latest available Nuget'
2222

2323
- script: |
24-
nuget install libpng-msvc14-x64 -ExcludeVersion -OutputDirectory "$(build.BinariesDirectory)"
2524
nuget install zlib-msvc14-x64 -ExcludeVersion -OutputDirectory "$(build.BinariesDirectory)"
26-
echo ##vso[task.prependpath]$(build.BinariesDirectory)\libpng-msvc14-x64\build\native\bin_release
2725
echo ##vso[task.prependpath]$(build.BinariesDirectory)\zlib-msvc14-x64\build\native\bin_release
28-
echo ##vso[task.setvariable variable=CL]/I$(build.BinariesDirectory)\libpng-msvc14-x64\build\native\include /I$(build.BinariesDirectory)\zlib-msvc14-x64\build\native\include
29-
echo ##vso[task.setvariable variable=LINK]/LIBPATH:$(build.BinariesDirectory)\libpng-msvc14-x64\build\native\lib_release /LIBPATH:$(build.BinariesDirectory)\zlib-msvc14-x64\build\native\lib_release
3026
3127
displayName: 'Install dependencies with nuget'
3228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Matplotlib now uses Pillow to save and read pngs
2+
````````````````````````````````````````````````
3+
4+
The builtin png encoder and decoder has been removed, and Pillow is now a
5+
dependency.

doc/devel/min_dep_policy.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ minimum Python and numpy.
5858
System and C-dependencies
5959
=========================
6060

61-
For system or c-dependencies (libpng, freetype, GUI frameworks, latex,
61+
For system or c-dependencies (freetype, GUI frameworks, latex,
6262
gs, ffmpeg) support as old as practical. These can be difficult to
6363
install for end-users and we want to be usable on as many systems as
6464
possible. We will bump these on a case-by-case basis.

lib/matplotlib/animation.py

-4
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,6 @@ def cleanup(self):
543543
class PillowWriter(MovieWriter):
544544
@classmethod
545545
def isAvailable(cls):
546-
try:
547-
import PIL
548-
except ImportError:
549-
return False
550546
return True
551547

552548
def __init__(self, *args, **kwargs):

lib/matplotlib/backend_bases.py

+11-27
Original file line numberDiff line numberDiff line change
@@ -53,42 +53,37 @@
5353
from matplotlib.transforms import Affine2D
5454
from matplotlib.path import Path
5555

56-
try:
57-
from PIL import PILLOW_VERSION
58-
from distutils.version import LooseVersion
59-
if LooseVersion(PILLOW_VERSION) >= "3.4":
60-
_has_pil = True
61-
else:
62-
_has_pil = False
63-
del PILLOW_VERSION
64-
except ImportError:
65-
_has_pil = False
6656

6757
_log = logging.getLogger(__name__)
68-
6958
_default_filetypes = {
70-
'ps': 'Postscript',
7159
'eps': 'Encapsulated Postscript',
60+
'jpg': 'Joint Photographic Experts Group',
61+
'jpeg': 'Joint Photographic Experts Group',
7262
'pdf': 'Portable Document Format',
7363
'pgf': 'PGF code for LaTeX',
7464
'png': 'Portable Network Graphics',
65+
'ps': 'Postscript',
7566
'raw': 'Raw RGBA bitmap',
7667
'rgba': 'Raw RGBA bitmap',
7768
'svg': 'Scalable Vector Graphics',
78-
'svgz': 'Scalable Vector Graphics'
69+
'svgz': 'Scalable Vector Graphics',
70+
'tif': 'Tagged Image File Format',
71+
'tiff': 'Tagged Image File Format',
7972
}
80-
81-
8273
_default_backends = {
83-
'ps': 'matplotlib.backends.backend_ps',
8474
'eps': 'matplotlib.backends.backend_ps',
75+
'jpg': 'matplotlib.backends.backend_agg',
76+
'jpeg': 'matplotlib.backends.backend_agg',
8577
'pdf': 'matplotlib.backends.backend_pdf',
8678
'pgf': 'matplotlib.backends.backend_pgf',
8779
'png': 'matplotlib.backends.backend_agg',
80+
'ps': 'matplotlib.backends.backend_ps',
8881
'raw': 'matplotlib.backends.backend_agg',
8982
'rgba': 'matplotlib.backends.backend_agg',
9083
'svg': 'matplotlib.backends.backend_svg',
9184
'svgz': 'matplotlib.backends.backend_svg',
85+
'tif': 'matplotlib.backends.backend_agg',
86+
'tiff': 'matplotlib.backends.backend_agg',
9287
}
9388

9489

@@ -1604,17 +1599,6 @@ class FigureCanvasBase:
16041599
fixed_dpi = None
16051600

16061601
filetypes = _default_filetypes
1607-
if _has_pil:
1608-
# JPEG support
1609-
register_backend('jpg', 'matplotlib.backends.backend_agg',
1610-
'Joint Photographic Experts Group')
1611-
register_backend('jpeg', 'matplotlib.backends.backend_agg',
1612-
'Joint Photographic Experts Group')
1613-
# TIFF support
1614-
register_backend('tif', 'matplotlib.backends.backend_agg',
1615-
'Tagged Image File Format')
1616-
register_backend('tiff', 'matplotlib.backends.backend_agg',
1617-
'Tagged Image File Format')
16181602

16191603
@cbook._classproperty
16201604
def supports_blit(cls):

0 commit comments

Comments
 (0)