Python3.7 练习题(三) 将指定目录下的图片进行批量尺寸大小处理

2023-06-14,,

# 将指定目录下的图片进行批量尺寸大小处理

#修改图片尺寸  导入Image os 快捷键 alt+enter
import os
from PIL import Image def process_image(filename,width = ,hight = ):
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('.')[-] in ext:
process_image(file)

Python3.7 练习题(三) 将指定目录下的图片进行批量尺寸大小处理的相关教程结束。

《Python3.7 练习题(三) 将指定目录下的图片进行批量尺寸大小处理.doc》

下载本文的Word格式文档,以方便收藏与打印。