import os

from PIL import Image, ImageFile
from PIL.Image import Resampling


def main():
# 压缩单张图片
outfile = r'X:\原件.jpg' # 单张图片地址

# 复制出来一个压缩文件
outfile_copy = outfile.replace('.jpg', '_压缩后的文件.jpg').replace('.png', '_压缩后的文件.png')
if os.path.exists(outfile_copy):
os.remove(outfile_copy)
os.system('copy {} {}'.format(outfile, outfile_copy))

compress_image(outfile_copy,mb=1000)

pass


# 压缩图片文件
def compress_image(outfile, mb=100, quality=85, k=0.9): # 通常你只需要修改mb大小
"""不改变图片尺寸压缩到指定大小
:param outfile: 压缩文件保存地址
:param mb: 压缩目标,KB
:param k: 每次调整的压缩比率
:param quality: 初始压缩比率
:return: 压缩文件地址,压缩文件大小
"""

o_size = os.path.getsize(outfile) // 1024 # 函数返回为字节,除1024转为kb(1kb = 1024 bit)
print('before_size:{} after_size:{}'.format(o_size, mb))
if o_size <= mb:
return outfile

ImageFile.LOAD_TRUNCATED_IMAGES = True # 防止图像被截断而报错

while o_size > mb:
im = Image.open(outfile)
x, y = im.size
out = im.resize((int(x * k), int(y * k)), Resampling.LANCZOS) # 最后一个参数设置可以提高图片转换后的质量
try:
out.save(outfile, quality=quality) # quality为保存的质量,从1(最差)到95(最好),此时为85
except Exception as e:
print(e)
break
o_size = os.path.getsize(outfile) // 1024


if __name__ == '__main__':
main()