From a053d154e96d2fe78e640ae5618efc636a8287d3 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 5 Jan 2016 10:42:47 -0500 Subject: [PATCH] Fix #5520: Don't round up if already rounded --- lib/matplotlib/image.py | 4 +++- lib/matplotlib/tests/test_image.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index fecb1691ded1..a36468866388 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -273,7 +273,9 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, # to round up the output width to the next integer. This also # means scaling the transform just slightly to account for the # extra subpixel. - if t.is_affine and round_to_pixel_border: + if (t.is_affine and round_to_pixel_border and + (out_width_base % 1.0 != 0.0 or + out_height_base % 1.0 != 0.0)): out_width = int(ceil(out_width_base) + 1) out_height = int(ceil(out_height_base) + 1) extra_width = (out_width - out_width_base) / out_width_base diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index ede3443a4c78..114bb0cd9707 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -580,6 +580,29 @@ def test_rotate_image(): ax1.set_ylim(0, 4) +@cleanup +def test_image_preserve_size2(): + n = 7 + data = np.identity(n, float) + + fig = plt.figure(figsize=(n, n), frameon=False) + + ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0]) + ax.set_axis_off() + fig.add_axes(ax) + ax.imshow(data, interpolation='nearest', origin='lower',aspect='auto') + buff = io.BytesIO() + fig.savefig(buff, dpi=1) + + buff.seek(0) + img = plt.imread(buff) + + assert img.shape == (7, 7, 4) + + assert_array_equal(np.asarray(img[:, :, 0], bool), + np.identity(n, bool)[::-1]) + + if __name__=='__main__': import nose nose.runmodule(argv=['-s','--with-doctest'], exit=False)