python實現(xiàn)將文件夾內(nèi)的每張圖片批量分割成多張
一、說在前面
需求:有一張長為960,寬為96的圖片,需要將其分割成10張96*96的圖片并存放在另外一個文件夾下,通過手工分割耗時且不規(guī)范,選擇python寫一個簡單的程序完成。
二、源碼
# -*- coding: utf-8 -*- """ Created on Thu Aug 23 18:19:09 2018 @author: Administrator """ import os from PIL import Image # 切割圖片 def splitimage(src, rownum, colnum, dstpath): img = Image.open(src) w, h = img.size if rownum <= h and colnum <= w: print('Original image info: %sx%s, %s, %s' % (w, h, img.format, img.mode)) print('圖片切割') s = os.path.split(src) if dstpath == '': dstpath = s[0] fn = s[1].split('.') basename = fn[0] ext = fn[-1] num = 0 rowheight = h // rownum colwidth = w // colnum for r in range(rownum): for c in range(colnum): box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight) img.crop(box).save(os.path.join(dstpath, basename + '_' + str(num) + '.' + ext), ext) num = num + 1 print('共生成 %s 張小圖片。' % num) else: print('error') # 創(chuàng)建文件夾 def mkdir(path): # 去除首位空格 path = path.strip() # 去除尾部 \ 符號 path = path.rstrip("\\") # 判斷路徑是否存在 # 存在 True # 不存在 False isExists = os.path.exists(path) # 判斷結(jié)果 if not isExists: os.makedirs(path) print (path+' 創(chuàng)建成功') return True else: print (path + ' 目錄已存在') return False folder = r'C:/Users/Administrator/Desktop/testresults' # 存放圖片的文件夾 path = os.listdir(folder) # print(path) for each_bmp in path: # 批量操作 first_name, second_name = os.path.splitext(each_bmp) each_bmp = os.path.join(folder, each_bmp) src = each_bmp print(src) print(first_name) # 定義要創(chuàng)建的目錄 mkpath = "C:/Users/Administrator/Desktop/results/"+ first_name # 調(diào)用函數(shù) mkdir(mkpath) if os.path.isfile(src): dstpath = mkpath if (dstpath == '') or os.path.exists(dstpath): row = int(1) # 切割行數(shù) col = int(10) # 切割列數(shù) if row > 0 and col > 0: splitimage(src, row, col, dstpath) else: print('無效的') else: print('圖片保存目錄 %s 不存在!' % dstpath) else: print('圖片文件 %s 不存在!' % src)
三、寫在后面
這里定義了切割行數(shù)和列數(shù):
如果需要將圖片更改尺寸,可以簡單的使用PIL庫中的resize()函數(shù),代碼如下:
from PIL import Image for i in range(1,100): img = Image.open("C:/Users/Administrator/Desktop/test_results/"+str(i)+".png") img = img.convert("L") img = img.resize((960,96)) img.save("C:/Users/Administrator/Desktop/test_results/"+str(i)+".png", "PNG")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于pytest結(jié)合csv模塊實現(xiàn)csv格式的數(shù)據(jù)驅(qū)動問題
這篇文章主要介紹了pytest結(jié)合csv模塊實現(xiàn)csv格式的數(shù)據(jù)驅(qū)動,使用python中的csv模塊來處理csv文件,結(jié)合pygtest的參數(shù)化處理方式來實現(xiàn)ddt,本文通過示例代碼給大家介紹的非常詳細,需要的朋友參考下吧2022-05-05python dataframe向下向上填充,fillna和ffill的方法
今天小編就為大家分享一篇python dataframe向下向上填充,fillna和ffill的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Python學(xué)習(xí)之Anaconda的使用與配置方法
我在學(xué)習(xí)Python的爬蟲框架中看到看到了anaconda的介紹,簡直是相見恨晚啊,我覺的每個Python的學(xué)習(xí)網(wǎng)站上首先都應(yīng)該使用anaconda來進行教程,因為在實踐的過程中光環(huán)境的各種報錯就能消磨掉你所有的學(xué)習(xí)興趣2018-01-01python 三種方法實現(xiàn)對Excel表格的讀寫
這篇文章主要介紹了python 三種方法實現(xiàn)對Excel表格的讀寫,幫助大家更好的利用python處理表格,感興趣的朋友可以了解下2020-11-11哈工大自然語言處理工具箱之ltp在windows10下的安裝使用教程
這篇文章主要介紹了哈工大自然語言處理工具箱之ltp在windows10下的安裝使用教程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05用Python的SimPy庫簡化復(fù)雜的編程模型的介紹
這篇文章主要介紹了用Python的SimPy庫簡化復(fù)雜的編程模型的介紹,本文來自于官方的開發(fā)者技術(shù)文檔,需要的朋友可以參考下2015-04-04