python 批量等比修改檔案夾下圖片的尺寸
最近在做實驗時,需要批量更改檔案夾中的影像尺寸,在網上查找了方法,以此記錄學習的程序,
文章目錄
- python 批量等比修改檔案夾下圖片的尺寸
- 前言
- 一、分別使用OpenCV和Pillow庫批量處理影像尺寸?
- 二、使用步驟
- 1.Opencv
- 2.Pillow
前言
提示:這里可以添加本文要記錄的大概內容:
例如:隨著人工智能的不斷發展,機器學習這門技術也越來越重要,很多人都開啟了學習機器學習,本文就介紹了機器學習的基礎內容,
提示:以下是本篇文章正文內容,下面案例可供參考
一、分別使用OpenCV和Pillow庫批量處理影像尺寸?
兩個庫都是影像處理中比較長用的,但是在使用時還是有一定的差異性,比如OpenCV中,讀取的路徑不能有中文格式,而pillow可以讀取中文路徑下的圖片,因此保存兩個代碼以備后續使用,
二、使用步驟
1.Opencv
代碼如下:
import cv2
import os.path
import os
import numpy as np
##長邊設定為512像素,等比修改圖片大小
##
def img_resize(img):
height, width = img.shape[0], img.shape[1]
# 設定新的圖片解析度框架,這里設定為長邊像素大小為512
width_new = 512
height_new = 512
# 判斷圖片的長寬比率
if width / height >= width_new / height_new:
img_new = cv2.resize(img, (width_new, int(height * width_new / width)))
else:
img_new = cv2.resize(img, (int(width * height_new / height), height_new))
return img_new
def read_path(file_path,save_path):
#遍歷該目錄下的所有圖片檔案
for filename in os.listdir(file_path):
# print(filename)
img = cv2.imread(file_path+'/'+ filename)
if img is None :
print("圖片更改完畢")
break
####change to size
image = img_resize(img)
cv2.imwrite(save_path + filename, image)
#讀取的目錄
if __name__ == '__main__':
file_path = 'E:/Images/LowResolution512/01'
save_path = 'E:/Images/LowResolution512/01/'
read_path(file_path,save_path)
當讀取的圖片路徑有中文時,會有如圖所示的錯誤,

2.Pillow
代碼如下:
# -*- coding: utf-8 -*-
import os
import glob
from PIL import Image
import os.path
'''修改圖片檔案大小、file_path:檔案夾路徑;jpgfile:圖片檔案;savedir:修改后要保存的路徑'''
def convertjpg(jpgfile, savedir, width_new=512, height_new=512):
img = Image.open(jpgfile)
width,height = img.size
#判斷長寬比
if width / height >= width_new / height_new:
new_img = img.resize((width_new, int(height * width_new / width)))
else:
new_img = img.resize((int(width * height_new / height), height_new))
return new_img.save(os.path.join(savedir, os.path.basename(jpgfile)))
'''查找給定路徑下圖片檔案,并修改其大小'''
def modifyjpgSize(file, saveDir):
for jpgfile in glob.glob(file):
convertjpg(jpgfile, saveDir)
# 讀取目錄
if __name__ == '__main__':
img_file = r'E:\Huc\風格調色圖片\中式-紅\轉檔\*.jpg'
saveDir = r'E:\Images\LowResolution512\05'
modifyjpgSize(img_file, saveDir)
不使用glob庫
# -*- coding: utf-8 -*-
import os
from PIL import Image
import os.path
'''修改圖片檔案大小、file_path:檔案夾路徑;jpgfile:圖片檔案;savedir:修改后要保存的路徑'''
def convertjpg(file_path, jpgfile, savedir, width_new=512, height_new=512):
img = Image.open(file_path + jpgfile)
width,height = img.size
#判斷長寬比
if width / height >= width_new / height_new:
new_img = img.resize((width_new, int(height * width_new / width)))
else:
new_img = img.resize((int(width * height_new / height), height_new))
return new_img.save(os.path.join(savedir, jpgfile))
'''回圈遍歷路徑下圖片檔案,并修改其大小'''
def modifyjpgSize(file_path, saveDir):
filelist = os.listdir(file_path)
for jpgfile in filelist:
convertjpg(file_path, jpgfile, saveDir)
#讀取目錄
if __name__ == '__main__':
file_path = 'E:/Huca/風格調色圖片/中式-紅/轉檔/'
saveDir = 'F:/ganggang/hucai/test1'
modifyjpgSize(file_path, saveDir)
使用這種方法可以很快的更改圖片尺寸,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/302321.html
標籤:其他
上一篇:如何用c#呼叫c++的dll傳遞各種稀奇古怪的引數。
下一篇:【20210920】HMM入門
