# 將指定目錄下的圖片進行批量尺寸大小處理 #修改圖片尺寸 導入Image os 快捷鍵 alt+enter import os from PIL import Image def process_image(filename,width = 640,hight = 1136): image = Image.open(filename) image_width = image.width image_height = image.height if image_width <= width and image_height <= hight: print(filename," is ok") return if 1.0*image_width/width > 1.0*image_height/hight: scale = 1.0 * image_width/width new_image = image.resize((int(image_width/scale),int(image_height/scale)),Image.ANTIALIAS) else: scale = 1.0 * image_height/hight new_image = image.resize((int(image_width / scale), int(image_height / scale)), Image.ANTIALIAS) new_image.save("new--"+filename) new_image.close()
#獲取目錄下面的文件的後綴 ext = ['jpg','png','jpeg'] files = os.listdir('.') for file in files: if file.split('.')[-1] in ext: process_image(file)