import matplotlib.pyplot as plt
from PIL import Image
import random
image_path = "./data/Set5/baby.png"
def random_crop(image, crop_shape):
###image 是pil读取的,crop_shape是裁剪的大小
nw = random.randint(0, image.size[0] - crop_shape[0]) ##裁剪图像在原图像中的坐标
nh = random.randint(0, image.size[1] - crop_shape[1])
image_crop = image.crop((nh, nw, nh + crop_shape[0], nw + crop_shape[1]))
return image_crop
if __name__ == "__main__":
image = Image.open(image_path)
print(image.size)
print(image.width)
hr= random_crop(image, [96,96])
lr= hr.resize((hr.width // 2, hr.height // 2), resample=Image.BICUBIC)
print(hr.size)
print(lr.size)
plt.figure()
plt.subplot(221)
plt.imshow(hr)
plt.title("hr")
plt.subplot(222)
plt.imshow(lr)
plt.title("lr")
plt.show()
python 实现图片的随机裁剪
最新推荐文章于 2024-04-10 15:22:42 发布