python实现对任意大小图片均匀切割的示例

yipeiwu_com6年前Python基础

改代码是在windows 系统下

打开路径和保存路径换成自己的就可以啦~

import numpy as np
import matplotlib
import os
 
def img_seg(dir):
  files = os.listdir(dir)
  for file in files:
    a, b = os.path.splitext(file)
    img = Image.open(os.path.join(dir + "\\" + file))
    hight, width = img.size
    w = 256
    id = 1
    i = 0
    while (i + w <= hight):
      j = 0
      while (j + w <= width):
        new_img = img.crop((i, j, i + w, j + w))
        #rename = "D:\\labelme\\images\\"
        rename = "D:\\labelme\\annotations\\"
        new_img.save(rename + a + "_" + str(id) + b)
        id += 1
        j += w
      i = i + w
 
 
if __name__ == '__main__':
  #path = "D:\\labelme\\data\\images\\train"
  path = "D:\\labelme\\data\\dataset_png"
  img_seg(path)

以上这篇python实现对任意大小图片均匀切割的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中shapefile转换geojson的示例

shapefile转换geojson import shapefile import codecs from json import dumps # read the shapefi...

python实现word 2007文档转换为pdf文件

在开发过程中,会遇到在命令行下将DOC文档(或者是其他Office文档)转换为PDF的要求。比如在项目中如果手册是DOC格式的,在项目发布时希望将其转换为PDF格式,并且保留DOC中的书...

pytorch 加载(.pth)格式的模型实例

pytorch 加载(.pth)格式的模型实例

有一些非常流行的网络如 resnet、squeezenet、densenet等在pytorch里面都有,包括网络结构和训练好的模型。 pytorch自带模型网址:https://pyto...

简单介绍Python中的round()方法

 round()方法返回 x 的小数点四舍五入到n个数字。 语法 以下是round()方法的语法: round( x [, n] ) 参数  &nbs...

简述Python2与Python3的不同点

在Python2和Python3中都提供print()方法来打印信息,但两个版本间的print稍微有差异 主要体现在以下几个方面: 1.python3中print是一个内置函数,有多个参...