[SciPy-user] Equivalent in Scipy of Matlab resize

Pauli Virtanen pav@iki...
Wed Mar 25 12:39:22 CDT 2009


Wed, 25 Mar 2009 13:13:40 -0400, David Warde-Farley wrote:
> On 25-Mar-09, at 10:43 AM, Nicolas ROUX wrote:
>> I am looking for the equivalent of Matlab resize in Scipy. Matlab's
>> resize is able to upscale/downscale an array with 'linear', 'bilinear',
>> 'bicubic'.
>> Giving a scale factor or the required output shape. I can't find the
>> equivalent function in Scipy. Does it exist ?
> 
> As far as I know, it doesn't; I thought something like that existed in
> scipy.ndimage.interpolation, but apparently I was wrong.

There's `ndimage.zoom` but it doesn't allow zooming different axes 
separately. But you can use `ndimage.affine_transform`:

----------------------------
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt
import numpy as np

def imresize(a, nx, ny, **kw):
    return ndimage.affine_transform(
        a, [(a.shape[0]-1)*1.0/nx, (a.shape[1]-1)*1.0/ny],
        output_shape=[nx,ny], **kw)

a = np.zeros((5,5))
a[2,2] = 1

plt.subplot(121)
plt.imshow(a, interpolation='nearest')
plt.subplot(122)
plt.imshow(imresize(a, 50, 50, order=1), interpolation='nearest')
plt.show()
----------------------------

Seems to work, but no guarantees.

-- 
Pauli Virtanen



More information about the SciPy-user mailing list