from PIL import Image
import torch
import random
crop_size = 128
# 图片数量为200
for i in range(200):
print(i)
img_index = random.randint(1, 800) # 大图的数量
img = Image.open('./DIV2K_gray' + "/" + str(img_index) + '.png')
img_H = img.size[1]
img_W = img.size[0]
H_start = random.randint(0, img_H - crop_size)
W_start = random.randint(0, img_W - crop_size)
crop_box = (W_start, H_start, W_start + crop_size, H_start + crop_size)
img_crop = img.crop(crop_box)
img_crop.save('./train_data' + "/" + str(i +1) + '.png')
PIL从大图裁剪小图片
最新推荐文章于 2024-07-25 22:02:25 发布