ktianc 5abaef08cc 1. 修复:移动端头像剪切功能
2. 更新:升级PC端vue-element-plus-admin版本到1.9.2
3. 更新:文件上传接口
4. 新增:kinit-api 命令创建app目录
2023-01-28 12:00:23 +08:00

53 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PIL import Image, ExifTags # 安装依赖包pip3 install pillow
from utils.file.compress import dynamic_quality
import os
import time
"""
PIL读取的图像发生自动旋转https://blog.csdn.net/mizhenpeng/article/details/82794112
使用python批量压缩图片文件https://blog.csdn.net/weixin_41855010/article/details/120723943
"""
def compress_jpg_png(filename, originpath):
name = filename.rstrip('.png').rstrip('.jpg')
im = Image.open(os.path.join(originpath, filename))
# 解决图像方向问题
try:
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == 'Orientation': break
exif = dict(im._getexif().items())
if exif[orientation] == 3:
im = im.rotate(180, expand=True)
elif exif[orientation] == 6:
im = im.rotate(270, expand=True)
elif exif[orientation] == 8:
im = im.rotate(90, expand=True)
except:
pass
# print(im.format,im.size,im.mode)
im = im.convert('RGB')
im.format = "JPEG"
new_photo = im.copy()
new_photo.thumbnail(im.size, resample=Image.ANTIALIAS)
save_args = {'format': im.format}
# print(save_args)
# if im.format=='JPEG':
# save_args['quality']=20
save_args['quality'], value = dynamic_quality.jpeg_dynamic_quality(im)
save_args['optimize'] = True
save_args['progressive=True'] = True
# print("JPEG Quality Changed")
# elif im.format=='PNG':
# save_args['format']='JPEG'
# save_args['quality']=5
# print("PNG Quality Changed")
new_file = os.path.join(originpath, name + str(int(time.time())) + ".jpg")
new_photo.save(new_file, **save_args)
return new_file
if __name__ == '__main__':
print(compress_jpg_png("1.jpg", "E:\\test"))