网络编程
位置:首页>> 网络编程>> Python编程>> python将原图裁剪为固定尺寸小图

python将原图裁剪为固定尺寸小图

作者:劲酒奶奶  发布时间:2022-01-11 00:51:51 

标签: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)

运算结果

运行前

python将原图裁剪为固定尺寸小图

运行过程

python将原图裁剪为固定尺寸小图

运行结果

python将原图裁剪为固定尺寸小图

来源:https://blog.csdn.net/qq_44813407/article/details/115315648

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com