【Python】如何在文件夹里批量分割图片?
生活随笔
收集整理的这篇文章主要介绍了
【Python】如何在文件夹里批量分割图片?
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1.Introduction
又是一年四季在轮回,而我终于入门了深度学习!
话说上周终于在Paddle框架下跑通了目标检测,激动了好几天,哈哈哈,深度学习的门槛是真的高(我是真的菜),由于拍的照片有点大,所以就需要对图片进行切割,切成2×2,3×3之类的。开始是是想用Matlab的,毕竟现在最熟悉的就是Matlab了,不过转念一想,都深度学习了,还是用Python吧~
2.Materials and methods
本文所实现的目的是对一个文件内的所有照片,进行批量切割,其中切割的个数可以自己选择,比如横着切2块,竖着切3块。
话不多说,上代码
# -*- coding: utf-8 -*- """ Created on Sun Dec 13 21:49:55 2020@author: YaoYee """import cv2 import os# Cutting the input image to h*w blocks heightCutNum = 2; widthCutNum = 2;# The folder path of input and output inPath = "C:/Users/YaoYee/Desktop/original/" outPath = "C:/Users/YaoYee/Desktop/cutting/"for f in os.listdir(inPath):path = inPath + f.strip()print(path)img = cv2.imread(path) # The size of each input imageheight = img.shape[0]width = img.shape[1]# The size of block that you want to cutheightBlock = int(height / heightCutNum)widthBlock = int(width / widthCutNum)for i in range(0,heightCutNum):for j in range(0,widthCutNum):cutImage = img[i*heightBlock:(i+1)*heightBlock, j*widthBlock:(j+1)*widthBlock]savePath = outPath + f.strip()[0:5] + "_" + str(i) + str(j) + ".jpg"cv2.imwrite(savePath,cutImage)emmm,注释啥的都写了,也没啥说的了吧
3. Results and discussion
运行下看看效果,这里我们把巴基和路飞大卸四块~
注:输出文件夹路径自己先构建好,也就是告诉代码把结果存到哪
4. Conclusion
I have nothing to loss
猜你喜欢:👇🏻
⭐【Python】如何在文件夹里批量替换文本中的内容?
⭐【Python】如何在文件夹里批量修改文件名(001-100)?
⭐【Python】随机划分数据集并生成VOC格式列表
总结
以上是生活随笔为你收集整理的【Python】如何在文件夹里批量分割图片?的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 计算机内存延迟,CPU性能差距竟然在这里
- 下一篇: python模拟抛硬币_python实现