def preprocess_image_crop(image_path, img_size):
'''
Preprocess the image scaling it so that its smaller size is img_size.
The larger size is then cropped in order to produce a square image.
'''
img = load_img(image_path)
scale = float(img_size) / min(img.size)
new_size = (int(np.ceil(scale * img.size[0])), int(np.ceil(scale * img.size[1])))
# print('old size: %s,new size: %s' %(str(img.size), str(new_size)))
img = img.resize(new_size, resample=Image.BILINEAR)
img = img_to_array(img)
crop_h = img.shape[0] - img_size
crop_v = img.shape[1] - img_size
img = img[crop_h:img_size+crop_h, crop_v:img_size+crop_v, :]
img = np.expand_dims(img, axis=0)
img = vgg16.preprocess_input(img)
return img
# util function to open, resize and format pictures into appropriate tensors
评论列表
文章目录