def zoom_image(image, zoom, out_width=25):
"""Return rescaled and cropped image array with width out_width.
"""
if zoom < 1:
raise ValueError("Zoom scale factor must be at least 1.")
width, height = image.shape
#if width < out_width:
# raise ValueError(
# "image width before zooming ({0}) is less "
# "than requested output width ({1})".format(width, out_width))
out_height = int(np.rint(float(out_width * height) / width))
t_width = int(np.rint(out_width * zoom))
t_height = int(np.rint(out_height * zoom))
if t_width // 2 != out_width // 2:
t_width += 1
if t_height // 2 != out_height // 2:
t_height += 1
# zoom with cubic interpolation
t_image = transform.resize(image, (t_width, t_height), order=3)
# crop
return t_image[(t_width - out_width) / 2:(t_width + out_width) / 2,
(t_height - out_height) / 2:(t_height + out_height) / 2]
评论列表
文章目录