題目:你有一個目錄,裝了很多照片,把它們的尺寸變成都不大于 iPhone5 解析度的大小,
代碼:
from PIL import Image
import os
from sys import argv
script, input_dir, output_dir = argv # 從命令列取得輸入檔案夾和輸出檔案夾
def convert_size(input_dir, output_dir, width, height):
path = input_dir
files = os.listdir(path) # 獲得輸入檔案夾中所有的檔案名稱
for file in files: # 逐個處理
if not os.path.isdir(file): # 如果不是檔案夾,則進行處理
img = Image.open(path + '/' + file) # 打開圖片
new_img = img.resize((width, height)) # 更改圖片尺寸
new_img.save(os.path.join(output_dir, file)) # 保存到輸出檔案夾
if __name__ == '__main__':
convert_size(input_dir, output_dir, 1100, 800)
原圖:

處理后:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/192014.html
標籤:Python
下一篇:Qt5+MSVC2015 32位編譯器報錯: qalgorithms.h: error C3615 不會生成常數運算式
