python实现原图裁剪为固定尺寸小图的具体代码,供大家参考,具体内容如下
讲解
1、代码效果:实现原图裁剪为固定尺寸小图
代码
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)
运算结果
运行前
运行过程
运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。