使用Python PIL庫讀取文件批量處理圖片大小實現(xiàn)
python 批量處理圖片大小
寫博客,文檔的時候常常要附上很多圖片,但是圖片的尺寸往往不符合我們的要求,手動一個一個修改尺寸再保存,太浪費時間了,既然學了 Python,當然就要物盡其用。本文實現(xiàn)了批量修改同級目錄下的 image\src 文件夾下的所有圖片,并輸出到 image\output 文件夾下,可以設定具體像素,也可以只設定水平像素或者垂直像素, 另一邊按原圖比例進行縮放。使用 Python 處理圖片大小需要使用 PIL 庫,PIL:Python Imaging Library,是一個圖片處理庫,需要自行安裝。
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,自動使用原圖比例和設定的豎直長度計算該值。
- set_vertical_length 希望修改成的豎直長度,如果為 0,自動使用原圖比例和設定的水平長度計算該值。
- img.size 包含了兩個值,比如(1080, 1920),第一個值是寬度(水平長度),第二值是高度(豎直長度)。
- set_img_type 如果為空,保持原圖片類型,如果非空則保存成對應圖片類型,比如
set_img_type = "png",會把圖片以 png 數(shù)據(jù)格式進行保存。
PIL 批量處理圖片
上面已經(jīng)介紹了處理單張圖片的方法,想要處理文件夾下的所有圖片就要能遍歷文件夾里所有的文件。這里使用遞歸遍歷出所有的文件,再分別進行處理。
使用遞歸遍歷文件夾
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)代碼簡介:
判斷當前文件夾中的每個元素,如果是文件夾則進入該文件夾遞歸調(diào)用,并重復讀取判斷操作。如果是文件,則在代碼中的 TODO 標簽處進行相應處理。注:這里默認 image\src 文件夾下不會出現(xiàn)非圖片文件。
準備輸出工作
目標是把修改后的圖片輸出到 image\output 文件夾下,這里需要做三件事,在 output 目錄下創(chuàng)建對應 src 目錄下的子文件夾,生成圖片保存的文件名(帶路徑),修改生成圖片的后綴名。將遞歸代碼修改如下,增加了 find_last_point 函數(shù)用于找到后綴名前的小數(shù)點來修改后綴名。
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 文件夾會越來越臃腫,想要找到轉(zhuǎn)換的圖片還需要花額外的時間,所以這里選擇程序剛開始就刪除整個 output 文件夾,并新建一個空的 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 后綴,所以如果設置生成 jpg 文件,程序會自動修改成 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庫讀取文件批量處理圖片大小實現(xiàn)的詳細內(nèi)容,更多關(guān)于Python PIL處理圖片大小的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于Django框架的關(guān)系模型序列化和一對多關(guān)系中的序列化解析
序列化的意思是把字典的形式轉(zhuǎn)化成Json格式。當我們展示數(shù)據(jù)的時候需要使用,反序列化的話,就是Json轉(zhuǎn)成字典形式,存儲數(shù)據(jù)時候使用,需要的朋友可以參考下2023-05-05
如何基于Python + requests實現(xiàn)發(fā)送HTTP請求
這篇文章主要介紹了如何基于Python + requests實現(xiàn)發(fā)送HTTP請求,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01
Python數(shù)據(jù)分析基礎之異常值檢測和處理方式
這篇文章主要介紹了Python數(shù)據(jù)分析基礎之異常值檢測和處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
使用ffmpeg-python分割視頻文件的實現(xiàn)方法
你是否遇到過需要將大型視頻按時間、大小或場景自動拆分的情況?無論是處理長視頻素材、制作短視頻片段,還是需要將視頻分割成特定大小以便于存儲和傳輸,本文將介紹如何使用ffmpeg-python庫實現(xiàn)視頻的智能分割2025-11-11
詳解tensorflow訓練自己的數(shù)據(jù)集實現(xiàn)CNN圖像分類
本篇文章了tensorflow訓練自己的數(shù)據(jù)集實現(xiàn)CNN圖像分類,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02

