def crop_image(image, shape):
factor = float(min(shape[:2])) / min(image.shape[:2])
new_size = [int(image.shape[0] * factor), int(image.shape[1] * factor)]
if new_size[0] < shape[0]:
new_size[0] = shape[0]
if new_size[1] < shape[0]:
new_size[1] = shape[0]
resized_image = transform.resize(image, new_size)
sample = np.asarray(resized_image) * 256
if shape[0] < sample.shape[0] or shape[1] < sample.shape[1]:
xx = int((sample.shape[0] - shape[0]))
yy = int((sample.shape[1] - shape[1]))
x_start = xx / 2
y_start = yy / 2
x_end = x_start + shape[0]
y_end = y_start + shape[1]
sample = sample[x_start:x_end, y_start:y_end, :]
return sample
评论列表
文章目录