3333
3434# map interpolation strings to module constants
3535_interpd_ = {
36+ 'antialiased' : _image .NEAREST , # this will use nearest or Hanning...
3637 'none' : _image .NEAREST , # fall back to nearest when not supported
3738 'nearest' : _image .NEAREST ,
3839 'bilinear' : _image .BILINEAR ,
@@ -168,11 +169,34 @@ def _resample(
168169 allocating the output array and fetching the relevant properties from the
169170 Image object *image_obj*.
170171 """
172+
173+ # decide if we need to apply anti-aliasing if the data is upsampled:
174+ # compare the number of displayed pixels to the number of
175+ # the data pixels.
176+ interpolation = image_obj .get_interpolation ()
177+ if interpolation == 'antialiased' :
178+ # don't antialias if upsampling by an integer number or
179+ # if zooming in more than a factor of 3
180+ shape = list (data .shape )
181+ if image_obj .origin == 'upper' :
182+ shape [0 ] = 0
183+ dispx , dispy = transform .transform ([shape [1 ], shape [0 ]])
184+
185+ if ((dispx > 3 * data .shape [1 ] or
186+ dispx == data .shape [1 ] or
187+ dispx == 2 * data .shape [1 ]) and
188+ (dispy > 3 * data .shape [0 ] or
189+ dispy == data .shape [0 ] or
190+ dispy == 2 * data .shape [0 ])):
191+ interpolation = 'nearest'
192+ else :
193+ interpolation = 'hanning'
194+
171195 out = np .zeros (out_shape + data .shape [2 :], data .dtype ) # 2D->2D, 3D->3D.
172196 if resample is None :
173197 resample = image_obj .get_resample ()
174198 _image .resample (data , out , transform ,
175- _interpd_ [image_obj . get_interpolation () ],
199+ _interpd_ [interpolation ],
176200 resample ,
177201 alpha ,
178202 image_obj .get_filternorm (),
@@ -432,7 +456,6 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
432456 A_scaled += 0.1
433457 # resample the input data to the correct resolution and shape
434458 A_resampled = _resample (self , A_scaled , out_shape , t )
435-
436459 # done with A_scaled now, remove from namespace to be sure!
437460 del A_scaled
438461 # un-scale the resampled data to approximately the
@@ -690,9 +713,10 @@ def get_interpolation(self):
690713 """
691714 Return the interpolation method the image uses when resizing.
692715
693- One of 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36',
694- 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom',
695- 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos', or 'none'.
716+ One of 'antialiased', 'nearest', 'bilinear', 'bicubic', 'spline16',
717+ 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
718+ 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos',
719+ or 'none'.
696720
697721 """
698722 return self ._interpolation
@@ -708,9 +732,9 @@ def set_interpolation(self, s):
708732
709733 Parameters
710734 ----------
711- s : {'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', \
712- 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian ', \
713- 'bessel', 'mitchell', 'sinc', 'lanczos', 'none'}
735+ s : {'antialiased', ' nearest', 'bilinear', 'bicubic', 'spline16',
736+ 'spline36', ' hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', \
737+ 'gaussian', ' bessel', 'mitchell', 'sinc', 'lanczos', 'none'}
714738
715739 """
716740 if s is None :
0 commit comments