Skip to content

Commit 5553dac

Browse files
committed
ENH: anti-alias down-sampled images
1 parent 9faf231 commit 5553dac

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

lib/matplotlib/axes/_axes.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5435,7 +5435,8 @@ def get_interp_point(ind):
54355435
def imshow(self, X, cmap=None, norm=None, aspect=None,
54365436
interpolation=None, alpha=None, vmin=None, vmax=None,
54375437
origin=None, extent=None, shape=None, filternorm=1,
5438-
filterrad=4.0, imlim=None, resample=None, url=None, **kwargs):
5438+
filterrad=4.0, imlim=None, resample=None,
5439+
antialiased=True, url=None, **kwargs):
54395440
"""
54405441
Display an image, i.e. data on a 2D regular raster.
54415442
@@ -5604,7 +5605,8 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
56045605
self.set_aspect(aspect)
56055606
im = mimage.AxesImage(self, cmap, norm, interpolation, origin, extent,
56065607
filternorm=filternorm, filterrad=filterrad,
5607-
resample=resample, **kwargs)
5608+
resample=resample, antialiased=antialiased,
5609+
**kwargs)
56085610

56095611
im.set_data(X)
56105612
im.set_alpha(alpha)

lib/matplotlib/image.py

+26
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def __init__(self, ax,
188188
filternorm=True,
189189
filterrad=4.0,
190190
resample=False,
191+
antialiased=True,
191192
**kwargs
192193
):
193194
"""
@@ -214,6 +215,7 @@ def __init__(self, ax,
214215
self.set_interpolation(interpolation)
215216
self.set_resample(resample)
216217
self.axes = ax
218+
self._antialiased = antialiased
217219

218220
self._imcache = None
219221

@@ -232,6 +234,22 @@ def get_size(self):
232234

233235
return self._A.shape[:2]
234236

237+
def get_antialiased(self):
238+
"""
239+
Get whether down-sampled images are low-pass filtered before
240+
down-sampling (i.e. when there is more data than pixels).
241+
"""
242+
return self._antialiased
243+
244+
def set_antialiased(self, antialiased):
245+
"""
246+
Set if an anti-aliasing (low-pass) filter is applied to the data
247+
if the image will down-sample the data (i.e. if the there is
248+
more data than pixels). If *True*, and the data is down-sampled,
249+
any user supplied filters are ignored, and a Lanczos filter is
250+
applied with radius `2 * M_data / M_image`.
251+
"""
252+
235253
def set_alpha(self, alpha):
236254
"""
237255
Set the alpha value used for blending - not supported on all backends.
@@ -422,6 +440,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
422440
A_scaled += 0.1
423441
A_resampled = np.zeros((out_height, out_width),
424442
dtype=A_scaled.dtype)
443+
if self.get_antialiased() and A.shape[0] > out_height:
444+
self.set_interpolation('lanczos')
445+
self.set_filterrad(2.0 * A.shape[0] / out_height )
446+
425447
# resample the input data to the correct resolution and shape
426448
_image.resample(A_scaled, A_resampled,
427449
t,
@@ -819,6 +841,7 @@ def __init__(self, ax,
819841
filternorm=1,
820842
filterrad=4.0,
821843
resample=False,
844+
antialiased=True,
822845
**kwargs
823846
):
824847

@@ -847,6 +870,7 @@ def __init__(self, ax,
847870
filternorm=filternorm,
848871
filterrad=filterrad,
849872
resample=resample,
873+
antialiased=antialiased,
850874
**kwargs
851875
)
852876

@@ -1266,6 +1290,7 @@ def __init__(self, bbox,
12661290
filternorm=1,
12671291
filterrad=4.0,
12681292
resample=False,
1293+
antialiased=True,
12691294
interp_at_native=True,
12701295
**kwargs
12711296
):
@@ -1293,6 +1318,7 @@ def __init__(self, bbox,
12931318
filternorm=filternorm,
12941319
filterrad=filterrad,
12951320
resample=resample,
1321+
antialiased=antialiased,
12961322
**kwargs
12971323
)
12981324

lib/matplotlib/offsetbox.py

+2
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,7 @@ def __init__(self, arr,
12601260
filternorm=1,
12611261
filterrad=4.0,
12621262
resample=False,
1263+
antialiased=True,
12631264
dpi_cor=True,
12641265
**kwargs
12651266
):
@@ -1275,6 +1276,7 @@ def __init__(self, arr,
12751276
filternorm=filternorm,
12761277
filterrad=filterrad,
12771278
resample=resample,
1279+
antialiased=antialiased,
12781280
**kwargs
12791281
)
12801282

0 commit comments

Comments
 (0)