# 将指定目录下的图片进行批量尺寸大小处理 #修改图片尺寸 导入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)