From 436dd6e5dd7dc8818284c8406c4eab6bdade1ee7 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 25 Oct 2019 12:52:59 -0700 Subject: [PATCH] FIX: fix anti-aliasing zoom bug --- lib/matplotlib/image.py | 10 ++++------ lib/matplotlib/tests/test_image.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 86391d4314eb..db3bf36cfee3 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -176,11 +176,10 @@ def _resample( if interpolation == 'antialiased': # don't antialias if upsampling by an integer number or # if zooming in more than a factor of 3 - shape = list(data.shape) - if image_obj.origin == 'upper': - shape[0] = 0 - dispx, dispy = transform.transform([shape[1], shape[0]]) - + pos = np.array([[0, 0], [data.shape[1], data.shape[0]]]) + disp = transform.transform(pos) + dispx = np.abs(np.diff(disp[:, 0])) + dispy = np.abs(np.diff(disp[:, 1])) if ((dispx > 3 * data.shape[1] or dispx == data.shape[1] or dispx == 2 * data.shape[1]) and @@ -190,7 +189,6 @@ def _resample( interpolation = 'nearest' else: interpolation = 'hanning' - out = np.zeros(out_shape + data.shape[2:], data.dtype) # 2D->2D, 3D->3D. if resample is None: resample = image_obj.get_resample() diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 67ac022daa1f..f227f2d3ec6b 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -198,6 +198,24 @@ def test_imshow_upsample3(fig_test, fig_ref): axs.imshow(A, interpolation='nearest') +@check_figures_equal(extensions=['png']) +def test_imshow_zoom(fig_test, fig_ref): + # should be less than 3 upsample, so should be nearest... + np.random.seed(19680801) + dpi = 100 + A = np.random.rand(int(dpi * 3), int(dpi * 3)) + for fig in [fig_test, fig_ref]: + fig.set_size_inches(2.9, 2.9) + axs = fig_test.subplots() + axs.imshow(A, interpolation='nearest') + axs.set_xlim([10, 20]) + axs.set_ylim([10, 20]) + axs = fig_ref.subplots() + axs.imshow(A, interpolation='antialiased') + axs.set_xlim([10, 20]) + axs.set_ylim([10, 20]) + + @check_figures_equal() def test_imshow_pil(fig_test, fig_ref): style.use("default")