Python如何批量更改圖像尺寸統(tǒng)一大小
批量更改圖像尺寸統(tǒng)一大小
import os
from PIL import Image
import glob
def convertjpg(jpgfile,outdir,width=200,height=500):
img=Image.open(jpgfile)
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
for jpgfile in glob.glob(('/home/yangguide/Videos/images/*.png')):
convertjpg(jpgfile,"/home/yangguide/Videos/image_2")
知識(shí)點(diǎn)
圖像庫(kù)PIL(Python Image Library)是python的第三方圖像處理庫(kù),但是由于其強(qiáng)大的功能與眾多的使用人數(shù),幾乎已經(jīng)被認(rèn)為是python官方圖像處理庫(kù)了。
Image類是PIL中的核心類,你有很多種方式來(lái)對(duì)它進(jìn)行初始化,比如從文件中加載一張圖像,處理其他形式的圖像,或者是從頭創(chuàng)造一張圖像等。
Image模塊操作的基本方法都包含于此模塊內(nèi)。如open、save、conver、show…等方法。
1.加載圖像,使用Image類的open()函數(shù):
Image.open(jpgfile)
2.保存圖像,使用Image類的save()函數(shù):
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
3.os.path.basename()方法:
返回path最后的文件名, 如果path以’/'結(jié)尾,那么就會(huì)返回空值, 即os.path.split(path)的第二個(gè)元素。
示例:
>>> import os >>> path = '/Users/beazley/Data/data.csv' >>> os.path.basename(path) #Get the last component of the path 'data.csv'
4.img.resize((width,height),Image.BILINEAR) :
使用resize函數(shù)指定圖像的大小和質(zhì)量,第二個(gè)參數(shù)設(shè)置和含義如下圖:

5.glob.glob()與glob.iglob()的用法:
glob.glob()可同時(shí)獲取所有的匹配路徑,而glob.iglob()一次只能獲取一個(gè)匹配路徑。
將不同尺寸的圖片和xml標(biāo)簽縮放到統(tǒng)一尺寸,并重新命名存儲(chǔ)
分享一個(gè)比較實(shí)用的功能,改一下文件路徑和縮放尺寸即可適配成自己的。
適用于原來(lái)是不同尺寸的圖片,不好統(tǒng)一縮放的,只能放到一張統(tǒng)一大小的畫布里。
如果原來(lái)的圖片尺寸是一致的,請(qǐng)參考本人另一篇博客,自己找一下咯。
運(yùn)行環(huán)境:python3.5+
需要安裝一下opencv,如果有anaconda,執(zhí)行conda install opencv-python
# *_* coding : UTF-8 *_*
# 開發(fā)人員 :csu·pan-_-||
# 開發(fā)時(shí)間 :2020/11/09 16:40
# 文件名稱 :renameFile.py
# 開發(fā)工具 :PyCharm
# 功能描述 :將文件夾下的圖片全部縮放,裁減,并按新文件名存儲(chǔ)
import os
import cv2
path = 'E:/Projects/images' # 原文件夾路徑
newpath = 'E:/Projects/newimages' # 新文件夾路徑
files = os.listdir(path) # 獲取文件名列表
for i, file in enumerate(files): # 展開文件名的列表和索引
if file.endswith('.jpg'):
imgName = os.path.join(path, file) # 獲取文件完整路徑
img = cv2.imread(imgName) # 讀圖
imgNew = cv2.resize(img, (1200, 1200)) # 縮放
# imgNew = imgNew[60:552,:] # 截取一部分區(qū)域
newName = os.path.join(newpath, 'img_%03d'%(0+i)+'.jpg') # 設(shè)置新的文件名
print(newName)
cv2.imwrite(newName,imgNew) # 存儲(chǔ)按新文件名命令的圖片
后面來(lái)了新的需求,作為一個(gè)程序員,需求是永遠(yuǎn)要去滿足的??s放的同時(shí),需要保持原有比例,全部設(shè)置到一張800 * 800的全黑畫布上面,即補(bǔ)零操作,又重新調(diào)整了代碼,其中的核心部分就是判斷圖像的長(zhǎng)邊是否大于800,大于800則將800與長(zhǎng)邊的比值設(shè)置為縮放比例,小于800則原圖大小不變。需要導(dǎo)入一個(gè)新庫(kù)numpy,conda install numpy:
# *_* coding : UTF-8 *_*
# 開發(fā)人員 :csu·pan-_-||
# 開發(fā)時(shí)間 :2020/11/09 18:15
# 文件名稱 :renameFile.py
# 開發(fā)工具 :PyCharm
# 功能描述 :將文件夾下的圖片全部縮放(同時(shí)保持原有寬高比例),裁切,并按新文件名存儲(chǔ)
import os
import cv2
import numpy as np
path = r'E:\Projects\images' # 原文件夾路徑
newpath = r'E:\Projects\newimages' # 新文件夾路徑
files = os.listdir(path) # 獲取文件名列表
c_w ,c_h = 800,800 # 全黑畫布的大小
for i, file in enumerate(files):
img_zeros = np.zeros((c_w, c_h, 3), np.uint8) # 創(chuàng)建全黑的圖像
if file.endswith('.jpg'):
imgName = os.path.join(path, file) # 獲取文件完整路徑
img = cv2.imread(imgName) # 讀圖
h, w , _ = img.shape # 獲取圖像寬高
# 縮放圖像,寬高大于800的按長(zhǎng)邊等比例縮放,小于800的保持原圖像大?。?
if max(w,h) > c_w:
ratio = c_w / max(w,h)
imgcrop = cv2.resize(img, (round(w * ratio) , round(h * ratio)))
# 將裁切后的圖像復(fù)制進(jìn)全黑圖像里
img_zeros[0:round(h * ratio), 0:round(w * ratio)] = imgcrop
else:
img_zeros[0:h, 0:w] = img
# imgNew = imgNew[60:552,:] # 截取一部分
# 設(shè)置新的文件名:
newName = os.path.join(newpath, 'img_%03d'%(0+i)+'.jpg')
print(newName)
cv2.imwrite(newName,img_zeros) # 存儲(chǔ)按新文件名命令的圖片
如下所示,下面兩張(test1.jpg、test2.jpg)是原圖,上面兩張(img_001.jpg、img_002.jpg)是統(tǒng)一調(diào)整到800 * 800后的,保持了原有的寬高比,沿左上角鋪在黑色畫布上。

你以為結(jié)束了嗎?并沒有!又來(lái)了新的需求,程序員永遠(yuǎn)都有活干,最好干到不要失業(yè)O(∩_∩)O哈哈~ 想多了。
言歸正傳,標(biāo)注的xml文件需要同步修改,于是把代碼調(diào)整了一下:
# *_* coding : UTF-8 *_*
# 開發(fā)人員 :csu·pan-_-||
# 開發(fā)時(shí)間 :2020/11/09 18:15
# 文件名稱 :renameFile.py
# 開發(fā)工具 :PyCharm
# 功能描述 :將文件夾下的圖片全部縮放(同時(shí)保持原有寬高比例),裁切,并按新文件名存儲(chǔ)
# 同時(shí)調(diào)整xml里的坐標(biāo)信息
import os
import cv2
import numpy as np
import xml.etree.ElementTree as ET
path = r'C:\Users\Administrator\Desktop\test' # 原文件夾路徑
newpath = r'C:\Users\Administrator\Desktop\newtest' # 新文件夾路徑
c_w ,c_h = 800,800 # 全黑畫布的大小
def edit_xml(xml_file,ratio,i):
"""
修改xml文件
:param xml_file:xml文件的路徑
:return:
"""
all_xml_file = os.path.join(path, xml_file)
tree = ET.parse(all_xml_file)
objs = tree.findall('object')
for ix, obj in enumerate(objs):
type = obj.find('type').text
if type == 'bndbox':
obj_bnd = obj.find('bndbox')
obj_xmin = obj_bnd.find('xmin')
obj_ymin = obj_bnd.find('ymin')
obj_xmax = obj_bnd.find('xmax')
obj_ymax = obj_bnd.find('ymax')
xmin = float(obj_xmin.text)
ymin = float(obj_ymin.text)
xmax = float(obj_xmax.text)
ymax = float(obj_ymax.text)
obj_xmin.text = str(round(xmin * ratio))
obj_ymin.text = str(round(ymin * ratio))
obj_xmax.text = str(round(xmax * ratio))
obj_ymax.text = str(round(ymax * ratio))
elif type == 'robndbox':
obj_bnd = obj.find('robndbox')
obj_cx = obj_bnd.find('cx')
obj_cy = obj_bnd.find('cy')
obj_w = obj_bnd.find('w')
obj_h = obj_bnd.find('h')
obj_angle = obj_bnd.find('angle')
cx = float(obj_cx.text)
cy = float(obj_cy.text)
w = float(obj_w.text)
h = float(obj_h.text)
obj_cx.text = str(cx * ratio)
obj_cy.text = str(cy * ratio)
obj_w.text = str(w * ratio)
obj_h.text = str(h * ratio)
newfile = os.path.join(newpath, '%05d'%(0+i)+'.xml')
tree.write(newfile, method='xml', encoding='utf-8') # 更新xml文件
if __name__ == '__main__':
files = os.listdir(path) # 獲取文件名列表
for i, file in enumerate(files):
img_zeros = np.zeros((c_w, c_h, 3), np.uint8) # 創(chuàng)建全黑的圖像
if file.endswith('.jpg'):
imgName = os.path.join(path, file) # 獲取文件完整路徑
xml_file = file.replace('.jpg','.xml')
img = cv2.imread(imgName) # 讀圖
h, w , _ = img.shape # 獲取圖像寬高
# 縮放圖像,寬高大于800的按長(zhǎng)邊等比例縮放,小于800的保持原圖像大?。?
if max(w,h) > c_w:
ratio = c_w / max(w,h)
imgcrop = cv2.resize(img, (round(w * ratio) , round(h * ratio)))
# 將裁切后的圖像復(fù)制進(jìn)全黑圖像里
img_zeros[0:round(h * ratio), 0:round(w * ratio)] = imgcrop
edit_xml(xml_file, ratio, i)
else:
img_zeros[0:h, 0:w] = img
edit_xml(xml_file, 1, i)
# 設(shè)置新的文件名:
newName = os.path.join(newpath, '%05d'%(0+i)+'.jpg')
print(newName)
cv2.imwrite(newName,img_zeros) # 存儲(chǔ)按新文件名命令的圖片
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python完成15位18位身份證的互轉(zhuǎn)功能
這篇文章主要介紹了使用Python完成15位18位身份證的互轉(zhuǎn)功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
淺析pytest?鉤子函數(shù)?之初始鉤子和引導(dǎo)鉤子
這篇文章主要介紹了pytest?鉤子函數(shù)?之初始鉤子和引導(dǎo)鉤子,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
基于Python實(shí)現(xiàn)對(duì)Excel工作表中的數(shù)據(jù)進(jìn)行排序
在Excel中,排序是整理數(shù)據(jù)的一種重要方式,它可以讓你更好地理解數(shù)據(jù),本文將介紹如何使用第三方庫(kù)Spire.XLS?for?Python通過(guò)Python來(lái)對(duì)Excel中的數(shù)據(jù)進(jìn)行排序,需要的可以參考下2024-03-03
Python中的變量及簡(jiǎn)單數(shù)據(jù)類型應(yīng)用
這篇文章主要介紹了Python中的變量及簡(jiǎn)單數(shù)據(jù)類型應(yīng)用,簡(jiǎn)單的數(shù)據(jù)類型包括字符串和數(shù)字,更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-03-03
Python 爬蟲批量爬取網(wǎng)頁(yè)圖片保存到本地的實(shí)現(xiàn)代碼
這篇文章主要介紹了Python 爬蟲批量爬取網(wǎng)頁(yè)圖片保存到本地,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
python中如何實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用
這篇文章主要介紹了python中如何實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03

