From 08789dac2cf2f2572a5a65517bec8d0ca0be9aca Mon Sep 17 00:00:00 2001 From: Larry Bradley Date: Tue, 19 Dec 2017 15:19:35 -0500 Subject: [PATCH 1/3] Fix imsave output format when format keyword is None --- lib/matplotlib/image.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 8b630ce3e442..d556d74edcce 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -1351,9 +1351,9 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, from matplotlib.figure import Figure # Fast path for saving to PNG - if (format == 'png' or format is None or - isinstance(fname, six.string_types) and - fname.lower().endswith('.png')): + if (format == 'png' or (format is None and + isinstance(fname, six.string_types) and + fname.lower().endswith('.png'))): image = AxesImage(None, cmap=cmap, origin=origin) image.set_data(arr) image.set_clim(vmin, vmax) From facd9e9df90487d33cc6596e5a07dd5ddad5eeca Mon Sep 17 00:00:00 2001 From: Larry Bradley Date: Tue, 19 Dec 2017 15:22:05 -0500 Subject: [PATCH 2/3] Add test for imsave output format --- lib/matplotlib/tests/test_image.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 9dc83efe27dc..ac0354224152 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -176,6 +176,16 @@ def test_imsave_color_alpha(): assert_array_equal(data, arr_buf) +def test_imsave_format(tmpdir): + # Test imsave output format based on the input filename + # (i.e. when the "format" keyword is None). + import imghdr + + filename = str(tmpdir.join('test_imsave.jpg')) + plt.imsave(filename, np.zeros((10, 10))) + + assert imghdr.what(filename) == 'jpeg' + @image_comparison(baseline_images=['image_alpha'], remove_text=True) def test_image_alpha(): plt.figure() From ae0dca63497fc0d5d55fcd9bf86ca62443114eca Mon Sep 17 00:00:00 2001 From: Larry Bradley Date: Tue, 19 Dec 2017 16:22:09 -0500 Subject: [PATCH 3/3] Use svg in imsave format test --- lib/matplotlib/tests/test_image.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index ac0354224152..e56ac4cfbe93 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -179,12 +179,13 @@ def test_imsave_color_alpha(): def test_imsave_format(tmpdir): # Test imsave output format based on the input filename # (i.e. when the "format" keyword is None). - import imghdr - filename = str(tmpdir.join('test_imsave.jpg')) + filename = str(tmpdir.join('test_imsave.svg')) plt.imsave(filename, np.zeros((10, 10))) - assert imghdr.what(filename) == 'jpeg' + lines = open(filename).readlines() + assert lines[0].startswith('