python压缩图片--指定压缩大小且保真压缩

代码核心思路

  • 循环降低图片质量quality
  • 直至图片质量大小符合目标质量后退出循环

pillow压缩失真严重

'''
pip install pillow
'''
from io import BytesIO

from PIL import Image


def pic_compress(pic_path, out_path, target_size, quality=90, step=5):
    # 读取图片bytes
    with open(pic_path, 'rb') as f:
        pic_byte = f.read()

    current_size = len(pic_byte) / 1024  # 图片当前质量大小
    print("图片压缩前的大小为(KB):", current_size)
    while current_size > target_size:  # 当达到目标压缩大小后退出循环
        img = Image.open(BytesIO(pic_byte))
        buf = BytesIO()
        img.save(buf, format=img.format, quality=quality)
        pic_byte = buf.getvalue()
        if quality - step < 0:  # 以降低到最小质量
            break
        quality -= step
        current_size = len(pic_byte) / 1024

    # 保存图片
    with open(out_path, 'wb') as f:
        f.write(pic_byte)

    return len(pic_byte) / 1024


def main():
    pic_size = pic_compress('test.jpg', 'new_test.jpg', target_size=100)
    print("图片压缩后的大小为(KB):", pic_size)

if __name__ == '__main__':
    main()

pillow处理后和原图片的对比
python压缩图片--指定压缩大小且保真压缩_第1张图片

使用pillow压缩的图片不单止大小质量没达标而且处理后的图片失真不忍直视。

使用opencv保真压缩

'''
pip install numpy
pip install opencv-python
'''
from io import BytesIO

import cv2
import numpy as np


def pic_compress(pic_path, out_path, target_size=199, quality=90, step=5, pic_type='.jpg'):
    # 读取图片bytes
    with open(pic_path, 'rb') as f:
        pic_byte = f.read()

    img_np = np.frombuffer(pic_byte, np.uint8)
    img_cv = cv2.imdecode(img_np, cv2.IMREAD_ANYCOLOR)

    current_size = len(pic_byte) / 1024
    print("图片压缩前的大小为(KB):", current_size)
    while current_size > target_size:
        pic_byte = cv2.imencode(pic_type, img_cv, [int(cv2.IMWRITE_JPEG_QUALITY), quality])[1]
        if quality - step < 0:
            break
        quality -= step
        current_size = len(pic_byte) / 1024

    # 保存图片
    with open(out_path, 'wb') as f:
        f.write(BytesIO(pic_byte).getvalue())

    return len(pic_byte) / 1024


def main():
    pic_size = pic_compress('test.jpg', 'new_test.jpg', target_size=100)
    print("图片压缩后的大小为(KB):", pic_size)


if __name__ == '__main__':
    main()

opencv处理后和原图片的对比
python压缩图片--指定压缩大小且保真压缩_第2张图片

文章灵感

  • 想用python压缩图片然后网上搜,搜索结果90%都是python使用pillow的压缩。最主要是pillow压缩后失真严重,在图片压缩方面纯属是个摆设的功能。

你可能感兴趣的:(python,图片压缩,python,opencv,图像处理)