[Numpy-discussion] dedicated function for resize with averaging or rebin 2d arrays?

Zachary Pincus zachary.pincus at yale.edu
Fri Nov 11 10:07:35 EST 2011

Hi Andrea,

scipy.ndimage.zoom will do this nicely for magnification. (Just set the spline order to 0 to get nearest-neighbor interpolation; otherwise you can use higher orders for better smoothing.)

For decimation (zooming out) scipy.ndimage.zoom also works, but it's not as nice as a dedicated decimation filter that would average properly over the area that's being squeezed into a single output pixel. (You'd have to choose the spline order manually to approximate that.) I'm afraid I don't have enough signal-processing background to know how to write a proper general-purpose decimation filter -- basically, you convolve with whatever bandlimiting filter (e.g. a gaussian, or do it in the Fourier domain), then just do nearest-neighbor downsampling, but I'm never sure how to properly choose the filter parameters!

Between this and ndimage.zoom for magnifying, one could get together a much better "rebin" function that in the edge cases of integer magnification/minification should work the same as the IDL one. But the participants in the old discussion you highlighted seemed unhappy with the time/space used for proper decimation, so I'm not sure what really would be best. 


On Nov 11, 2011, at 1:41 AM, Andrea Zonca wrote:

> hi,
> I work in astrophysics where the most common programming language is
> currently IDL.
> A common request of people switching from IDL to python is the
> implementation of the REBIN function, which either downsizes a 2d
> array by averaging or increases its dimension by repeating its
> elements. In both cases the new shape must be an integer factor of the
> old shape.
> I believe it is a very handy function for quick smoothing of 2 dimensional data.
> I found a discussion about this topic in the archives:
> http://thread.gmane.org/gmane.comp.python.numeric.general/885/focus=894
> Do you think it would be useful to add such function to numpy?
> I created a simple implementation to help in the discussion:
> https://gist.github.com/1348792
> thanks,
> Andrea Zonca
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list