|
9 | 9 | import logging |
10 | 10 | from pathlib import Path |
11 | 11 | import urllib.parse |
12 | | -import urllib.request |
13 | 12 |
|
14 | 13 | import numpy as np |
15 | 14 |
|
|
22 | 21 |
|
23 | 22 | # For clarity, names from _image are given explicitly in this module: |
24 | 23 | import matplotlib._image as _image |
25 | | -import matplotlib._png as _png |
26 | 24 |
|
27 | 25 | # For user convenience, the names from _image are also imported into |
28 | 26 | # the image namespace: |
@@ -635,6 +633,7 @@ def contains(self, mouseevent): |
635 | 633 |
|
636 | 634 | def write_png(self, fname): |
637 | 635 | """Write the image to png file with fname""" |
| 636 | + from matplotlib import _png |
638 | 637 | im = self.to_rgba(self._A[::-1] if self.origin == 'lower' else self._A, |
639 | 638 | bytes=True, norm=True) |
640 | 639 | _png.write_png(im, fname) |
@@ -1359,7 +1358,11 @@ def imread(fname, format=None): |
1359 | 1358 | .. _Pillow documentation: http://pillow.readthedocs.io/en/latest/ |
1360 | 1359 | """ |
1361 | 1360 |
|
1362 | | - handlers = {'png': _png.read_png, } |
| 1361 | + def read_png(*args, **kwargs): |
| 1362 | + from matplotlib import _png |
| 1363 | + return _png.read_png(*args, **kwargs) |
| 1364 | + |
| 1365 | + handlers = {'png': read_png, } |
1363 | 1366 | if format is None: |
1364 | 1367 | if isinstance(fname, str): |
1365 | 1368 | parsed = urllib.parse.urlparse(fname) |
@@ -1396,7 +1399,8 @@ def imread(fname, format=None): |
1396 | 1399 | parsed = urllib.parse.urlparse(fname) |
1397 | 1400 | # If fname is a URL, download the data |
1398 | 1401 | if len(parsed.scheme) > 1: |
1399 | | - fd = BytesIO(urllib.request.urlopen(fname).read()) |
| 1402 | + from urllib import request |
| 1403 | + fd = BytesIO(request.urlopen(fname).read()) |
1400 | 1404 | return handler(fd) |
1401 | 1405 | else: |
1402 | 1406 | with open(fname, 'rb') as fd: |
@@ -1441,6 +1445,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, |
1441 | 1445 | resolution of the output image. |
1442 | 1446 | """ |
1443 | 1447 | from matplotlib.figure import Figure |
| 1448 | + from matplotlib import _png |
1444 | 1449 | if isinstance(fname, os.PathLike): |
1445 | 1450 | fname = os.fspath(fname) |
1446 | 1451 | if format is None: |
|
0 commit comments