8000 Simplify _png extension by handling file open/close in Python. by anntzer · Pull Request #15095 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Simplify _png extension by handling file open/close in Python. #15095

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
Aug 22, 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
3 changes: 2 additions & 1 deletion lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ def draw_image(self, gc, x, y, im, transform=None):
fname = os.path.splitext(os.path.basename(self.fh.name))[0]
fname_img = "%s-img%d.png" % (fname, self.image_counter)
self.image_counter += 1
_png.write_png(im[::-1], os.path.join(path, fname_img))
with pathlib.Path(path, fname_img).open("wb") as file:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite like assigning to builtin names, but acknowledge that there's already precedence for this in the library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a builtin name anymore...

_png.write_png(im[::-1], file)

# reference the image in the pgf picture
writeln(self.fh, r"\begin{pgfscope}")
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,20 +821,20 @@ def draw_image(self, gc, x, y, im, transform=None):
if url is not None:
self.writer.start('a', attrib={'xlink:href': url})
if rcParams['svg.image_inline']:
bytesio = io.BytesIO()
_png.write_png(im, bytesio)
oid = oid or self._make_id('image', bytesio.getvalue())
buf = _png.write_png(im, None)
oid = oid or self._make_id('image', buf)
attrib['xlink:href'] = (
"data:image/png;base64,\n" +
base64.b64encode(bytesio.getvalue()).decode('ascii'))
base64.b64encode(buf).decode('ascii'))
else:
if self.basename is None:
raise ValueError("Cannot save image data to filesystem when "
"writing SVG to an in-memory buffer")
filename = '{}.image{}.png'.format(
self.basename, next(self._image_counter))
_log.info('Writing image file for inclusion: %s', filename)
_png.write_png(im, filename)
with open(filename, 'wb') as file:
_png.write_png(im, file)
oid = oid or 'Im_' + self._make_id('image', filename)
attrib['xlink:href'] = filename

Expand Down
38 changes: 12 additions & 26 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ def write_png(self, fname):
from matplotlib import _png
im = self.to_rgba(self._A[::-1] if self.origin == 'lower' else self._A,
bytes=True, norm=True)
_png.write_png(im, fname)
with open(fname, "wb") as file:
_png.write_png(im, file)

def set_data(self, A):
"""
Expand Down Expand Up @@ -1382,12 +1383,6 @@ def imread(fname, format=None):

.. _Pillow documentation: http://pillow.readthedocs.io/en/latest/
"""

def read_png(*args, **kwargs):
from matplotlib import _png
return _png.read_png(*args, **kwargs)

handlers = {'png': read_png, }
if format is None:
if isinstance(fname, str):
parsed = urllib.parse.urlparse(fname)
Expand All @@ -1412,34 +1407,24 @@ def read_png(*args, **kwargs):
ext = 'png'
else:
ext = format

if ext not in handlers: # Try to load the image with PIL.
try:
if ext != 'png':
try: # Try to load the image with PIL.
from PIL import Image
except ImportError:
raise ValueError('Only know how to handle extensions: %s; '
'with Pillow installed matplotlib can handle '
'more images' % list(handlers))
raise ValueError('Only know how to handle PNG; with Pillow '
'installed, Matplotlib can handle more images')
with Image.open(fname) as image:
return pil_to_array(image)

handler = handlers[ext]

# To handle Unicode filenames, we pass a file object to the PNG
# reader extension, since Python handles them quite well, but it's
# tricky in C.
from matplotlib import _png
if isinstance(fname, str):
parsed = urllib.parse.urlparse(fname)
# If fname is a URL, download the data
if len(parsed.scheme) > 1:
from urllib import request
fd = BytesIO(request.urlopen(fname).read())
return handler(fd)
else:
with open(fname, 'rb') as fd:
return handler(fd)
else:
return handler(fname)
return _png.read_png(fd)
with cbook.open_file_cm(fname, "rb") as file:
return _png.read_png(file)


def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
Expand Down Expand Up @@ -1516,7 +1501,8 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
arr = arr[::-1]
rgba = sm.to_rgba(arr, bytes=True)
if format == "png" and pil_kwargs is None:
_png.write_png(rgba, fname, dpi=dpi, metadata=metadata)
with cbook.open_file_cm(fname, "wb") as file:
_png.write_png(rgba, file, dpi=dpi, metadata=metadata)
else:
try:
from PIL import Image
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3433,7 +3433,8 @@ def to_png(self, filename, texstr, color='black', dpi=120, fontsize=14):
from matplotlib import _png
rgba, depth = self.to_rgba(
texstr, color=color, dpi=dpi, fontsize=fontsize)
_png.write_png(rgba, filename)
with open(filename, "wb") as file:
_png.write_png(rgba, file)
return depth

def get_depth(self, texstr, dpi=120, fontsize=14):
Expand Down
17 changes: 10 additions & 7 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,10 @@ def compare_images(expected, actual, tol, in_decorator=False):
expected = convert(expected, True)

# open the image files and remove the alpha channel (if it exists)
expected_image = _png.read_png_int(expected)
actual_image = _png.read_png_int(actual)
expected_image = expected_image[:, :, :3]
actual_image = actual_image[:, :, :3]
with open(expected, "rb") as expected_file:
expected_image = _png.read_png_int(expected_file)[:, :, :3]
with open(actual, "rb") as actual_file:
actual_image = _png.read_png_int(actual_file)[:, :, :3]

actual_image, expected_image = crop_to_same(
actual, actual_image, expected, expected_image)
Expand Down Expand Up @@ -438,8 +438,10 @@ def save_diff_image(expected, actual, output):
'''
# Drop alpha channels, similarly to compare_images.
from matplotlib import _png
expected_image = _png.read_png( 1E79 expected)[..., :3]
actual_image = _png.read_png(actual)[..., :3]
with open(expected, "rb") as expected_file:
expected_image = _png.read_png(expected_file)[..., :3]
with open(actual, "rb") as actual_file:
actual_image = _png.read_png(actual_file)[..., :3]
actual_image, expected_image = crop_to_same(
actual, actual_image, expected, expected_image)
expected_image = np.array(expected_image).astype(float)
Expand All @@ -465,4 +467,5 @@ def save_diff_image(expected, actual, output):
# Hard-code the alpha channel to fully solid
save_image_np[:, :, 3] = 255

_png.write_png(save_image_np, output)
with open(output, "wb") as output_file:
_png.write_png(save_image_np, output_file)
8 changes: 5 additions & 3 deletions lib/matplotlib/tests/test_png.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from io import BytesIO
import glob
import os
from pathlib import Path

import numpy as np
import pytest

Expand Down Expand Up @@ -33,9 +35,9 @@ def test_pngsuite():

def test_imread_png_uint16():
from matplotlib import _png
img = _png.read_png_int(os.path.join(os.path.dirname(__file__),
'baseline_images/test_png/uint16.png'))

with (Path(__file__).parent
/ 'baseline_images/test_png/uint16.png').open('rb') as file:
img = _png.read_png_int(file)
assert (img.dtype == np.uint16)
assert np.sum(img.flatten()) == 134184960

Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/texmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ def get_grey(self, tex, fontsize=None, dpi=None):
alpha = self.grey_arrayd.get(key)
if alpha is None:
pngfile = self.make_png(tex, fontsize, dpi)
X = _png.read_png(os.path.join(self.texcache, pngfile))
with open(os.path.join(self.texcache, pngfile), "rb") as file:
X = _png.read_png(file)
self.grey_arrayd[key] = alpha = X[:, :, -1]
return alpha

Expand Down
Loading
0