1. 改变图片大小(byte)
def rebyte(pic_file, re_byte, quality_num=100):
"""
:param pic_file 图片文件 str 本地文件
:param re_byte 期望字节数 Kb
:param quality_num 保存质量
:return:
"""
new_image = Image.open(pic_file)
new_image = new_image.convert('RGB') # 需将格式统一转成 GRB 格式
pic_file = pic_file.replace('.png', '.jpg')
try:
new_image.save(pic_file, quality=quality_num)
while quality_num > 1 and os.path.getsize(pic_file) / float(1024) > int(re_byte):
# os.remove(pic_file)
quality_num -= 2 # 步长
new_image.save(pic_file, optimize=True, quality=quality_num)
except Exception as e:
print(f'rebit报错|{e}')
2. 改变图片尺寸
from PIL import Image
def picture_resize(original_pic_obj, target_size, resize_type=2):