python放大并按图片中心剪切、放大
缩小 | 原始大小 | 放大并剪切后 |
---|---|---|
放大
img=crop_image(cv2.resize(img, None, fx=1, fy=1, interpolation=cv2.INTER_CUBIC),new_height,new_width)
缩小
mini= cv2.resize(cv2.copyMakeBorder(img, P_A, P_A, P_A, P_A, cv2.BORDER_REPLICATE), (150, 150))
源码
其中有将矩阵转Image、Image转矩阵两个过程。
def crop_image(re_img,new_height,new_width):
re_img=Image.fromarray(np.uint8(re_img))
width, height = re_img.size
left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2
crop_im = re_img.crop((left, top, right, bottom)) #Cropping Image
crop_im = np.asarray(crop_im)
return crop_im
new_width = 150 #Enter the crop image width
new_height = 150 #Enter the crop image height
img = cv2.imread("learn/cats/cat.3.jpg")
img=cv2.resize(img,(150,150))
P_A=50
img=crop_image(cv2.resize(img, None, fx=1, fy=1, interpolation=cv2.INTER_CUBIC),new_height,new_width)
plt.imshow(img)
plt.title('cat')
plt.show()