Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
crop and resize numpy array
from scipy import interpolate
def crop_and_resample_2darray(arr, x_crop, y_crop, resample, *args, **kwargs):
"""Crop a 2darray and resize the data"""
len_x_crop = x_crop[1]-x_crop[0]
len_y_crop = y_crop[1]-y_crop[0]
arr_crop = arr[x_crop[0]:x_crop[1], y_crop[0]:y_crop[1]]
f = interpolate.interp2d(np.arange(len_y_crop),
*args, **kwargs)
result = f(np.arange(len_x_crop, step=len_x_crop/resample[1]),
np.arange(len_y_crop, step=len_y_crop/resample[0]))
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment