Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 238ac88

Browse files
committed
FIX: fix anti-aliasing zoom bug
1 parent 09c8381 commit 238ac88

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lib/matplotlib/image.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,10 @@ def _resample(
176176
if interpolation == 'antialiased':
177177
# don't antialias if upsampling by an integer number or
178178
# if zooming in more than a factor of 3
179-
shape = list(data.shape)
180-
if image_obj.origin == 'upper':
181-
shape[0] = 0
182-
dispx, dispy = transform.transform([shape[1], shape[0]])
183-
179+
pos = np.array([[0, 0],[data.shape[1], data.shape[0]]])
180+
disp = transform.transform(pos)
181+
dispx = np.abs(np.diff(disp[:, 0]))
182+
dispy = np.abs(np.diff(disp[:, 1]))
184183
if ((dispx > 3 * data.shape[1] or
185184
dispx == data.shape[1] or
186185
dispx == 2 * data.shape[1]) and
@@ -190,7 +189,6 @@ def _resample(
190189
interpolation = 'nearest'
191190
else:
192191
interpolation = 'hanning'
193-
194192
out = np.zeros(out_shape + data.shape[2:], data.dtype) # 2D->2D, 3D->3D.
195193
if resample is None:
196194
resample = image_obj.get_resample()

lib/matplotlib/tests/test_image.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ def test_imshow_upsample3(fig_test, fig_ref):
197197
axs.set_position([0, 0, 1, 1])
198198
axs.imshow(A, interpolation='nearest')
199199

200+
@check_figures_equal(extensions=['png'])
201+
def test_imshow_zoom(fig_test, fig_ref):
202+
# should be less than 3 upsample, so should be nearest...
203+
np.random.seed(19680801)
204+
dpi = 100
205+
A = np.random.rand(int(dpi * 3), int(dpi * 3))
206+
for fig in [fig_test, fig_ref]:
207+
fig.set_size_inches(2.9, 2.9)
208+
axs = fig_test.subplots()
209+
axs.imshow(A, interpolation='nearest')
210+
axs.set_xlim([10, 20])
211+
axs.set_ylim([10, 20])
212+
axs = fig_ref.subplots()
213+
axs.imshow(A, interpolation='antialiased')
214+
axs.set_xlim([10, 20])
215+
axs.set_ylim([10, 20])
200216

201217
@check_figures_equal()
202218
def test_imshow_pil(fig_test, fig_ref):

0 commit comments

Comments
 (0)