使用Python PIL庫讀取文件批量處理圖片大小實(shí)現(xiàn)
python 批量處理圖片大小
寫博客,文檔的時(shí)候常常要附上很多圖片,但是圖片的尺寸往往不符合我們的要求,手動一個(gè)一個(gè)修改尺寸再保存,太浪費(fèi)時(shí)間了,既然學(xué)了 Python,當(dāng)然就要物盡其用。本文實(shí)現(xiàn)了批量修改同級目錄下的 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,自動使用原圖比例和設(shè)定的豎直長度計(jì)算該值。
- set_vertical_length 希望修改成的豎直長度,如果為 0,自動使用原圖比例和設(shè)定的水平長度計(jì)算該值。
- img.size 包含了兩個(gè)值,比如(1080, 1920),第一個(gè)值是寬度(水平長度),第二值是高度(豎直長度)。
- set_img_type 如果為空,保持原圖片類型,如果非空則保存成對應(yīng)圖片類型,比如
set_img_type = "png"
,會把圖片以 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 文件夾下不會出現(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 文件夾會越來越臃腫,想要找到轉(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 文件,程序會自動修改成 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)文章
Django部署到服務(wù)器后無法獲取到靜態(tài)元素 The requested resource
寫了一個(gè)Django項(xiàng)目,部署到云主機(jī)后,訪問發(fā)現(xiàn)圖片無法訪問,報(bào)錯(cuò)The requested resource was not found on this server,下面給大家介紹Django部署到服務(wù)器后無法獲取到靜態(tài)元素The requested resource was not found on this server(問題及解決方案),需要的朋友可以參考下2024-02-02python實(shí)現(xiàn)Excel多行多列的轉(zhuǎn)換的示例
本文主要介紹了python實(shí)現(xiàn)Excel多行多列的轉(zhuǎn)換的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python跑循環(huán)時(shí)內(nèi)存泄露的解決方法
這篇文章主要介紹了Python跑循環(huán)時(shí)內(nèi)存泄露的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Python設(shè)計(jì)模式結(jié)構(gòu)型組合模式
這篇文章主要介紹了Python設(shè)計(jì)模式結(jié)構(gòu)型組合模式,組合模式即Composite?Pattern,將對象組合成成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu),組合模式使得用戶對單個(gè)對象和組合對象的使用具有一致性,下文具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-02-02Django和websocket實(shí)現(xiàn)簡單的多人聊天的示例代碼
本文主要介紹了使用Django和WebSocket實(shí)現(xiàn)一個(gè)簡單的多人聊天應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01tensorflow實(shí)現(xiàn)簡單的卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了tensorflow實(shí)現(xiàn)簡單的卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05python 實(shí)現(xiàn)循環(huán)定義、賦值多個(gè)變量的操作
這篇文章主要介紹了python 實(shí)現(xiàn)循環(huán)定義、賦值多個(gè)變量的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03