python实现原图裁剪为固定尺寸小图的具体代码,供大家参考,具体内容如下
讲解
1、代码效果:实现原图裁剪为固定尺寸小图
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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) |
运算结果
运行前
运行过程
运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_44813407/article/details/115315648