使用Python PIL庫讀取文件批量處理圖片大小實(shí)現(xiàn)
python 批量處理圖片大小
寫博客,文檔的時(shí)候常常要附上很多圖片,但是圖片的尺寸往往不符合我們的要求,手動(dòng)一個(gè)一個(gè)修改尺寸再保存,太浪費(fèi)時(shí)間了,既然學(xué)了 Python,當(dāng)然就要物盡其用。本文實(shí)現(xiàn)了批量修改同級(jí)目錄下的 image\src 文件夾下的所有圖片,并輸出到 image\output 文件夾下,可以設(shè)定具體像素,也可以只設(shè)定水平像素或者垂直像素, 另一邊按原圖比例進(jìn)行縮放。使用 Python 處理圖片大小需要使用 PIL 庫,PIL:Python Imaging Library,是一個(gè)圖片處理庫,需要自行安裝。
PIL 讀取圖片大小
下面是修改單張圖片尺寸的函數(shù)。
from PIL import Image
set_img_type = ""
def resize_image(filein, fileout, set_horizontal_length, set_vertical_length):
if set_vertical_length == 0 and set_horizontal_length == 0:
return
img = Image.open(filein)
img_size = img.size
img_horizontal_length = img_size[0]
img_vertical_length = img_size[1]
if set_vertical_length == 0:
set_vertical_length = set_horizontal_length * img_vertical_length / img_horizontal_length
if set_horizontal_length == 0:
set_horizontal_length = set_vertical_length * img_horizontal_length / img_vertical_length
print img_horizontal_length, img_vertical_length
print set_horizontal_length, set_vertical_length
# resize image with high-quality
out = img.resize((set_horizontal_length, set_vertical_length), Image.ANTIALIAS)
if set_img_type == "":
out.save(fileout)
else:
out.save(fileout, set_img_type)代碼簡介
- filein 圖片輸入文件名(帶路徑)。
- fileout 圖片輸出文件名(帶路徑)。
- set_horizontal_length 希望修改成的水平長度,如果為 0,自動(dòng)使用原圖比例和設(shè)定的豎直長度計(jì)算該值。
- set_vertical_length 希望修改成的豎直長度,如果為 0,自動(dòng)使用原圖比例和設(shè)定的水平長度計(jì)算該值。
- img.size 包含了兩個(gè)值,比如(1080, 1920),第一個(gè)值是寬度(水平長度),第二值是高度(豎直長度)。
- set_img_type 如果為空,保持原圖片類型,如果非空則保存成對應(yīng)圖片類型,比如
set_img_type = "png",會(huì)把圖片以 png 數(shù)據(jù)格式進(jìn)行保存。
PIL 批量處理圖片
上面已經(jīng)介紹了處理單張圖片的方法,想要處理文件夾下的所有圖片就要能遍歷文件夾里所有的文件。這里使用遞歸遍歷出所有的文件,再分別進(jìn)行處理。
使用遞歸遍歷文件夾
def check_image(root_dir):
if not os.path.isdir(root_dir):
return
for lists in os.listdir(root_dir):
path = os.path.join(root_dir, lists)
if os.path.isfile(path):
# TODO: resize image.
if os.path.isdir(path):
check_image(path)代碼簡介:
判斷當(dāng)前文件夾中的每個(gè)元素,如果是文件夾則進(jìn)入該文件夾遞歸調(diào)用,并重復(fù)讀取判斷操作。如果是文件,則在代碼中的 TODO 標(biāo)簽處進(jìn)行相應(yīng)處理。注:這里默認(rèn) image\src 文件夾下不會(huì)出現(xiàn)非圖片文件。
準(zhǔn)備輸出工作
目標(biāo)是把修改后的圖片輸出到 image\output 文件夾下,這里需要做三件事,在 output 目錄下創(chuàng)建對應(yīng) src 目錄下的子文件夾,生成圖片保存的文件名(帶路徑),修改生成圖片的后綴名。將遞歸代碼修改如下,增加了 find_last_point 函數(shù)用于找到后綴名前的小數(shù)點(diǎn)來修改后綴名。
import os
import os.path
def check_image(root_dir):
if not os.path.isdir(root_dir):
return
for lists in os.listdir(root_dir):
path = os.path.join(root_dir, lists)
# handle output path of dir and file
out_path = image_output_dir + path[9:]
print path
if os.path.isfile(path):
# handle output file name
point_position = find_last_point(out_path)
if not set_img_type == "":
out_path = out_path[0:point_position + 1] + set_img_type
print out_path
# resize image.
resize_image(path, out_path, horizontal_length, vertical_length)
if os.path.isdir(path):
# make dir in image\output
if not os.path.exists(out_path):
os.mkdir(out_path)
print out_path
check_image(path)
def find_last_point(file_path):
position = 0
temp = 0
while temp != -1:
temp = file_path.find(".")
if temp != -1:
position = temp
file_path = file_path[ temp + 1:]
return position清理 output 文件夾
如果只生成不清理,output 文件夾會(huì)越來越臃腫,想要找到轉(zhuǎn)換的圖片還需要花額外的時(shí)間,所以這里選擇程序剛開始就刪除整個(gè) output 文件夾,并新建一個(gè)空的 output 文件夾。
import os
import os.path
import shutil
def clear():
if os.path.exists(image_output_dir):
shutil.rmtree(image_output_dir)
if not os.path.exists(image_output_dir):
os.mkdir(image_output_dir)完整代碼
PIL 不支持修改為 jpg 后綴,所以如果設(shè)置生成 jpg 文件,程序會(huì)自動(dòng)修改成 jpeg 文件。
import os
import os.path
import shutil
from PIL import Image
horizontal_length = 260
vertical_length = 0
image_src_dir = r"image\src"
image_output_dir = r"image\output"
set_img_type = ""
# set_img_type = "bmp"
# set_img_type = "jpeg"
# set_img_type = "png"
def test_run():
if os.path.exists(image_output_dir):
shutil.rmtree(image_output_dir)
if not os.path.exists(image_output_dir):
os.mkdir(image_output_dir)
global set_img_type
if set_img_type == "jpg":
set_img_type = "jpeg"
check_image(image_src_dir)
print "finish."
def resize_image(filein, fileout, set_horizontal_length, set_vertical_length):
if set_vertical_length == 0 and set_horizontal_length == 0:
return
img = Image.open(filein)
img_size = img.size
img_horizontal_length = img_size[0]
img_vertical_length = img_size[1]
if set_vertical_length == 0:
set_vertical_length = set_horizontal_length * img_vertical_length / img_horizontal_length
if set_horizontal_length == 0:
set_horizontal_length = set_vertical_length * img_horizontal_length / img_vertical_length
print img_horizontal_length, img_vertical_length
print set_horizontal_length, set_vertical_length
# resize image with high-quality
out = img.resize((set_horizontal_length, set_vertical_length), Image.ANTIALIAS)
if set_img_type == "":
out.save(fileout)
else:
out.save(fileout, set_img_type)
def check_image(root_dir):
if not os.path.isdir(root_dir):
return
for lists in os.listdir(root_dir):
path = os.path.join(root_dir, lists)
# handle output path of dir and file
out_path = image_output_dir + path[9:]
print path
if os.path.isfile(path):
# handle output file name
point_position = find_last_point(out_path)
if not set_img_type == "":
out_path = out_path[0:point_position + 1] + set_img_type
print out_path
# resize image.
resize_image(path, out_path, horizontal_length, vertical_length)
if os.path.isdir(path):
# make dir in image\output
if not os.path.exists(out_path):
os.mkdir(out_path)
print out_path
check_image(path)
def find_last_point(file_path):
position = 0
temp = 0
i = 0
while temp != -1:
temp = file_path.find(".")
if temp != -1:
if i == 0:
position = position + temp
else:
position = position + temp + 1
file_path = file_path[temp+1:]
i = 1
return position
if __name__ == "__main__":
test_run()以上就是使用Python PIL庫讀取文件批量處理圖片大小實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python PIL處理圖片大小的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談numpy 中dot()函數(shù)的計(jì)算方式
這篇文章主要介紹了淺談numpy 中dot()函數(shù)的計(jì)算方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05
關(guān)于Django框架的關(guān)系模型序列化和一對多關(guān)系中的序列化解析
序列化的意思是把字典的形式轉(zhuǎn)化成Json格式。當(dāng)我們展示數(shù)據(jù)的時(shí)候需要使用,反序列化的話,就是Json轉(zhuǎn)成字典形式,存儲(chǔ)數(shù)據(jù)時(shí)候使用,需要的朋友可以參考下2023-05-05
如何基于Python + requests實(shí)現(xiàn)發(fā)送HTTP請求
這篇文章主要介紹了如何基于Python + requests實(shí)現(xiàn)發(fā)送HTTP請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Python數(shù)據(jù)分析基礎(chǔ)之異常值檢測和處理方式
這篇文章主要介紹了Python數(shù)據(jù)分析基礎(chǔ)之異常值檢測和處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
使用ffmpeg-python分割視頻文件的實(shí)現(xiàn)方法
你是否遇到過需要將大型視頻按時(shí)間、大小或場景自動(dòng)拆分的情況?無論是處理長視頻素材、制作短視頻片段,還是需要將視頻分割成特定大小以便于存儲(chǔ)和傳輸,本文將介紹如何使用ffmpeg-python庫實(shí)現(xiàn)視頻的智能分割2025-11-11
詳解tensorflow訓(xùn)練自己的數(shù)據(jù)集實(shí)現(xiàn)CNN圖像分類
本篇文章了tensorflow訓(xùn)練自己的數(shù)據(jù)集實(shí)現(xiàn)CNN圖像分類,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
Python+selenium破解拼圖驗(yàn)證碼的腳本
很多網(wǎng)站在登錄或者注冊時(shí)都會(huì)遇到拼圖驗(yàn)證碼,這種拼圖驗(yàn)證碼實(shí)際上是多個(gè)小碎片經(jīng)過重新組合成的一張整體。本文將和大家分享一個(gè)基于Python selenium的破解拼圖驗(yàn)證碼的腳本,需要的可以參考一下2022-02-02

