from PIL import Image
import os, sys
path = "./images/"
if not os.path.exists(path):
os.makedirs(path)
dirs = os.listdir(path)
final_size = 600;
def resize_aspect_fit():
for item in dirs:
if os.path.isfile(path + item):
im = Image.open(path + item)
f, e = os.path.splitext(path + item) #将文件名路径和扩展名分开, e.g. ('c:\window\python','.py')
size = im.size
ratio = float(final_size) / max(size)
new_image_size = tuple([int(x * ratio) for x in size])
im = im.resize(new_image_size, Image.ANTIALIAS)
new_im = Image.new("RGB", (final_size, final_size))
new_im.paste(im, ((final_size - new_image_size[0]) // 2, (final_size - new_image_size[1]) // 2))
new_im.save(f + '_resized.jpg', 'JPEG', quality=80)
#run the function
resize_aspect_fit()18
2021
04
Python compress images
作者:kerek | 分类:Programme | 浏览:134 | 评论:0
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。