I have a 2d numpy array (611,1024) of floats that I need to resize to 256,256. I'm trying to use interp2d.
Here is what I've tried (based on this answer):
import numpy as np
from scipy import interpolate
arr = np.random.random((611,1024))
W, H = arr.shape[:2]
new_W, new_H = (256,256)
xrange = lambda x: np.linspace(0, 1, x)
f = interpolate.interp2d(xrange(W), xrange(H), arr, kind="linear")
new_arr = f(xrange(new_W), xrange(new_H))
Here is the error message I get:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-61-90f66cfeb2d9> in <module>
5 xrange = lambda x: np.linspace(0, 1, x)
6
----> 7 f = interpolate.interp2d(xrange(W), xrange(H), img, kind="linear")
8 new_arr = f(xrange(new_W), xrange(new_H))
/usr/local/lib/python3.6/dist-packages/scipy/interpolate/interpolate.py in __init__(self, x, y, z, kind, copy, bounds_error, fill_value)
207 if z.ndim == 2:
208 if z.shape != (len(y), len(x)):
--> 209 raise ValueError("When on a regular grid with x.size = m "
210 "and y.size = n, if z.ndim == 2, then z "
211 "must have shape (n, m)")
ValueError: When on a regular grid with x.size = m and y.size = n, if z.ndim == 2, then z must have shape (n, m)
This answer helped me: how to interpolate a numpy array with linear interpolation
Can anyone point me in the right direction?