本文主要是介绍python实现对任意大小图片均匀切割,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
水贴啦~~
改代码是在windows 系统下
打开路径和保存路径换成自己的就可以啦~
import numpy as np
import matplotlib
import osdef 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.sizew = 256id = 1i = 0while (i + w <= hight):j = 0while (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 += 1j += wi = i + wif __name__ == '__main__':#path = "D:\\labelme\\data\\images\\train"path = "D:\\labelme\\data\\dataset_png"img_seg(path)
这篇关于python实现对任意大小图片均匀切割的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!