Skip to content

Commit bcd1d5d

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 0ac415f commit bcd1d5d

24 files changed

+239
-1031
lines changed

.appveyor.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ install:
6363
# https://github.com/conda-forge/conda-forge.github.io/issues/157#issuecomment-223536381
6464
#
6565
- conda create -q -n test-environment python=%PYTHON_VERSION%
66-
msinttypes freetype=2.6 "libpng>=1.6.21,<1.7" zlib=1.2 tk=8.5
66+
msinttypes freetype=2.6 zlib=1.2 tk=8.5
6767
pip setuptools numpy sphinx tornado
6868
- activate test-environment
6969
- echo %PYTHON_VERSION% %TARGET_ARCH%
@@ -85,11 +85,9 @@ test_script:
8585
# Now build the thing..
8686
- set LINK=/LIBPATH:%cd%\lib
8787
- pip install -ve .
88-
# these should show no z, png, or freetype dll...
88+
# these should show no z or freetype dll...
8989
- set "DUMPBIN=%VS140COMNTOOLS%\..\..\VC\bin\dumpbin.exe"
9090
- 'if x%MPLSTATICBUILD% == xTrue "%DUMPBIN%" /DEPENDENTS lib\matplotlib\ft2font*.pyd | findstr freetype.*.dll && exit /b 1 || exit /b 0'
91-
- 'if x%MPLSTATICBUILD% == xTrue "%DUMPBIN%" /DEPENDENTS lib\matplotlib\_png*.pyd | findstr z.*.dll && exit /b 1 || exit /b 0'
92-
- 'if x%MPLSTATICBUILD% == xTrue "%DUMPBIN%" /DEPENDENTS lib\matplotlib\_png*.pyd | findstr png.*.dll && exit /b 1 || exit /b 0'
9391

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

INSTALL.rst

Lines changed: 13 additions & 15 deletions
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
@@ -128,7 +128,6 @@ Matplotlib requires the following dependencies:
128128

129129
* `Python <https://www.python.org/downloads/>`_ (>= 3.6)
130130
* `FreeType <https://www.freetype.org/>`_ (>= 2.3)
131-
* `libpng <http://www.libpng.org>`_ (>= 1.2)
132131
* `NumPy <http://www.numpy.org>`_ (>= 1.11)
133132
* `setuptools <https://setuptools.readthedocs.io/en/latest/>`_
134133
* `cycler <http://matplotlib.org/cycler/>`_ (>= 0.10.0)
@@ -184,8 +183,8 @@ etc., you can install the following:
184183
.. _pkg-config: https://www.freedesktop.org/wiki/Software/pkg-config/
185184

186185
If not using pkg-config (in particular on Windows), you may need to set the
187-
include path (to the FreeType, libpng, and zlib headers) and link path (to
188-
the FreeType, libpng, and zlib libraries) explicitly, if they are not in
186+
include path (to the FreeType and zlib headers) and link path (to
187+
the FreeType and zlib libraries) explicitly, if they are not in
189188
standard locations. This can be done using standard environment variables
190189
-- on Linux and OSX:
191190

@@ -202,8 +201,8 @@ etc., you can install the following:
202201
set LINK=/LIBPATH:C:\directory\containing\freetype.lib ...
203202
204203
where ``...`` means "also give, in the same format, the directories
205-
containing ``png.h`` and ``zlib.h`` for the include path, and for
206-
``libpng.so``/``png.lib`` and ``libz.so``/``z.lib`` for the link path."
204+
containing ``zlib.h`` for the include path, and for
205+
``libz.so``/``z.lib`` for the link path."
207206

208207
.. note::
209208

@@ -244,20 +243,20 @@ Building on macOS
244243
-----------------
245244

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

256-
brew install libpng freetype pkg-config
255+
brew install freetype pkg-config
257256

258257
If you are using MacPorts, execute the following instead::
259258

260-
port install libpng freetype pkgconfig
259+
port install freetype pkgconfig
261260

262261
After installing the above requirements, install Matplotlib from source by
263262
executing::
@@ -281,7 +280,7 @@ https://packaging.python.org/guides/packaging-binary-extensions/#setting-up-a-bu
281280
for how to set up a build environment.
282281

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

287286
There are a few possibilities to build Matplotlib on Windows:
@@ -297,17 +296,16 @@ Wheel builds using conda packages
297296
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
298297

299298
This is a wheel build, but we use conda packages to get all the requirements.
300-
The binary requirements (png, FreeType,...) are statically linked and therefore
301-
not needed during the wheel install.
299+
FreeType is statically linked and therefore not needed during the wheel install.
302300

303301
Set up the conda environment. Note, if you want a qt backend, add ``pyqt`` to
304302
the list of conda packages.
305303

306304
::
307305

308-
conda create -n "matplotlib_build" python=3.7 numpy python-dateutil pyparsing tornado cycler tk libpng zlib freetype msinttypes
306+
conda create -n "matplotlib_build" python=3.7 numpy python-dateutil pyparsing tornado cycler tk zlib freetype msinttypes
309307
conda activate matplotlib_build
310-
# force the build against static libpng and zlib libraries
308+
# force the build against static zlib libraries
311309
set MPLSTATICBUILD=True
312310
python setup.py bdist_wheel
313311

ci/azure-pipelines-steps.yml

Lines changed: 0 additions & 4 deletions
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
Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 11 additions & 27 deletions
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)