import os
from PIL import Image
from PIL import ImageFile
import imghdr

def compress_image(outfile, mb=200, 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)), Image.ANTIALIAS)  # 最后一个参数设置可以提高图片转换后的质量
    try:
        out.save(outfile, quality=quality)  # quality为保存的质量,从1(最差)到95(最好),此时为85
    except Exception as e:
        print(e)
        break
    o_size = os.path.getsize(outfile) // 1024
 return outfile
def file_name(file_dir):
  for home,dirs,files in os.walk(file_dir):
    for filename in files:
      fullname = os.path.join(home,filename)
      imgType_list = {'jpg','bmp','png','jpeg','rgb','tif'}
      if imghdr.what(fullname) in imgType_list:
        compress_image(str(fullname))
file_name('/opt/tomcat_8580/webapps/file/upload/')