Skip to content

Instantly share code, notes, and snippets.

@J535D165
Created January 7, 2018 11:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save J535D165/a2ac7f27ad6ddd6ee85e7d30e9f4080e to your computer and use it in GitHub Desktop.
Save J535D165/a2ac7f27ad6ddd6ee85e7d30e9f4080e to your computer and use it in GitHub Desktop.
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),
np.arange(len_x_crop),
arr_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