def resize(orig, factor, method="nearest"):
"""
Scales a numpy array to a new size using a specified scaling method
:param orig: n-dimen numpy array to resize
:param factor: integer, double, or n-tuple to scale orig by
:param method: string, interpolation method to use when resizing. Options are "nearest",
"bilinear", and "cubic". Default is "nearest"
:return: n-dimen numpy array
"""
method_dict = {'nearest': 0, 'bilinear': 1, 'cubic': 2}
if method.lower() not in method_dict:
raise ValueError("Invalid interpolation method. Options are: " + ", ".join(method_dict.keys()))
try:
return zoom(orig, factor, order=method_dict[method.lower()])
except RuntimeError:
# raised by zoom when factor length does not match orig.shape length
raise ValueError("Factor sequence length does not match input length")
评论列表
文章目录