import numpy as np
import pandas as pd
import os
import torch as t
import torchvision.transforms.functional as ff
from torch.utils.data import Dataset
from PIL import Image
import torchvision.transforms as transforms
import cfg
def center_crop(data, crop_size):
data = ff.center_crop(data, crop_size)
return data
def img_crop(img):
higth, width = img.size
w = 100
id = 1
i = 0
while (i + w = higth):
j = 0
while (j + w = width):
new_img = img.crop((i, j, i + w, j + w))
rename = r""#小图存放路径
new_img.save(rename + str(id) + ".png", quality=95)#小图保存
id += 1
j += 100
print(new_img)
i += 100
if __name__ == "__main__":
path = r''#待处理图像
crop_size = [500, 500]#中心裁剪为500X500图像便于后面裁剪小图
img = Image.open(path)
print(img)
img = center_crop(img, crop_size)
print(img)
img_crop(img)