【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性能差距竟然在这里
- 下一篇: c调用java jar_C#调用java