def iaresize(f, new_shape):
from Numeric import array, asarray, ravel, reshape, ceil, concatenate, zeros, matrixmultiply, maximum, minimum, take
from LinearAlgebra import inverse
def calc(f, new_shape=new_shape):
from Numeric import array, ravel, reshape, ceil, concatenate, zeros, matrixmultiply, maximum, minimum, take
from LinearAlgebra import inverse
Sh = 1.* new_shape[0]/f.shape[0]
Sw = 1.* new_shape[1]/f.shape[1]
T = array([[Sw,0,0],[0,Sh,0],[0,0,1]])
faux = ravel(f)
if len(f.shape) == 1:
f = reshape(f, (1, f.shape[0]))
m, n = ceil(Sh*f.shape[0]), ceil(Sw*f.shape[1])
x, y = iameshgrid(range(n),range(m))
aux = concatenate((reshape(x, (1,m*n)), reshape(y, (1,m*n))))
aux = concatenate((aux, zeros((1, m*n))))
XY = matrixmultiply(inverse(T), aux+1)
X, Y = XY[0,:], XY[1,:]
X = maximum(1, minimum(f.shape[1], X))
Y = maximum(1, minimum(f.shape[0], Y))
XYi = iasub2ind(f.shape, map(round, Y-1), map(round, X-1))
g = take(faux, XYi)
g = reshape(g, (m,n))
g = g[0:new_shape[0], 0:new_shape[1]]
return g
f = asarray(f)
if len(f.shape) == 3: # imagem colorida
g = zeros(concatenate(([3],new_shape)))
for i in range(f.shape[0]):
g[i,:,:] = calc(f[i,:,:])
else:
g = calc(f)
return g