python原图裁剪为固定尺寸小图 python将原图裁剪为固定尺寸小图
劲酒奶奶 人气:0想了解python将原图裁剪为固定尺寸小图的相关内容吗,劲酒奶奶在本文为您仔细讲解python原图裁剪为固定尺寸小图的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python裁剪小图,python裁剪固定尺寸小图,python原图裁剪,下面大家一起来学习吧。
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)
运算结果
运行前
运行过程
运行结果
加载全部内容