学习日志(十五):图片裁剪crop

方法一:cv2

import cv2

img_path = './demo/inputs/birds.jpg'
img = cv2.imread(img_path)
print(type(img),img.shape)
crop = img[0:321, 0:321]
print(type(crop),crop.shape)
cv2.imshow('image',crop)
cv2.waitKey(0)

#结果
#<class 'numpy.ndarray'> (321, 481, 3)
#<class 'numpy.ndarray'> (321, 321, 3)

方法二:PIL+plt

import matplotlib.pyplot as plt
from PIL import Image
img_path = './demo/inputs/birds.jpg'
img = Image.open(img_path)
print(type(img),img.size)
crop = img.crop((0,0,300,300))
print(type(crop),crop.size)
plt.imshow(crop)
plt.show()
#结果
#<class 'PIL.JpegImagePlugin.JpegImageFile'> (481, 321)
#<class 'PIL.Image.Image'> (300, 300)

说明:方法一如果设置的裁剪尺寸大于原图最多只会显示原图的全部大小,而方法二会自动使用黑色填充

猜你喜欢

转载自blog.csdn.net/weixin_44825185/article/details/109443244