python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python压缩图片

从入门到实战详解Python压缩图片的完整方法

作者:detayun

这篇文章主要为大家详细介绍了Python实现压缩图片的常见方法,主要是Python库Pillow,OpenCV及tinify,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下

图片太大,上传慢、存储贵、加载卡?用 Python 几行代码就能搞定。

为什么要压缩图片

方案一:Pillow(最推荐,简单够用)

Pillow 是 Python 最主流的图像处理库,压缩图片只需几行代码。

1. 按质量压缩(有损)

from PIL import Image

def compress_image(input_path, output_path, quality=85):
    """
    quality: 1-100,越小文件越小,画质越差
    推荐值:70-85 之间性价比最高
    """
    img = Image.open(input_path)
    
    # JPEG 不支持透明通道,需要转换模式
    if img.mode in ('RGBA', 'P'):
        img = img.convert('RGB')
    
    img.save(output_path, 'JPEG', optimize=True, quality=quality)
    
    original_size = os.path.getsize(input_path) / 1024
    new_size = os.path.getsize(output_path) / 1024
    print(f"原大小: {original_size:.1f}KB → 压缩后: {new_size:.1f}KB,节省 {100 - new_size/original_size*100:.1f}%")

compress_image("photo.jpg", "photo_compressed.jpg", quality=80)

2. 按尺寸压缩(缩小分辨率)

def resize_image(input_path, output_path, max_width=1200):
    img = Image.open(input_path)
    
    w, h = img.size
    if w > max_width:
        ratio = max_width / w
        new_size = (max_width, int(h * ratio))
        img = img.resize(new_size, Image.LANCZOS)  # LANCZOS 是高质量缩放算法
    
    img.save(output_path, 'JPEG', optimize=True, quality=85)

3. 批量压缩整个文件夹

import os
from PIL import Image

def batch_compress(input_dir, output_dir, quality=80):
    os.makedirs(output_dir, exist_ok=True)
    
    for filename in os.listdir(input_dir):
        if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.webp')):
            input_path = os.path.join(input_dir, filename)
            output_path = os.path.join(output_dir, filename)
            
            img = Image.open(input_path)
            if img.mode in ('RGBA', 'P'):
                img = img.convert('RGB')
            img.save(output_path, 'JPEG', optimize=True, quality=quality)
            print(f"✅ {filename}")

batch_compress("./photos", "./photos_compressed", quality=75)

方案二:OpenCV(适合需要更多控制的场景)

import cv2

def compress_with_cv2(input_path, output_path, quality=80):
    img = cv2.imread(input_path)
    # quality 参数范围 0-100
    cv2.imwrite(output_path, img, [cv2.IMWRITE_JPEG_QUALITY, quality])

OpenCV 优势是可以顺便做裁剪、旋转、加水印等操作,再一起压缩。

方案三:tinify(API 压缩,效果最好)

如果追求极致压缩比,可以用 tinify 的 API,它用的是有损+无损混合算法。

import tinify

tinify.key = "你的API_KEY"
source = tinify.from_file("photo.jpg")
source.to_file("photo_tiny.jpg")

免费额度每月 500 张,超出后付费,但压缩效果确实比 Pillow 好 20%-40%。

几个实用技巧

技巧说明
quality=85 是 sweet spot低于 70 肉眼可见模糊,高于 90 压缩效果很弱
先缩尺寸再压质量效果翻倍,比如先 resize 到 1200px 宽,再 quality=80
PNG 转 JPEG如果不需要透明通道,PNG 转 JPEG 体积能缩小 70%+
用 WebP 格式同等画质下比 JPEG 小 30%,现代浏览器全支持
optimize=True 别忘加Pillow 的这个参数会额外做一次无损优化,白捡的压缩

到底选哪个?

需求推荐方案
快速搞定、批量处理Pillow
要同时做图像处理(裁剪/旋转)OpenCV
追求最小体积、不差钱tinify API
面向 Web 前端输出转为 WebP 格式

完整实战脚本(直接复制就能用)

import os
from PIL import Image

def smart_compress(input_path, output_path, max_width=1920, quality=80):
    img = Image.open(input_path)
    
    # 1. 转换模式
    if img.mode in ('RGBA', 'P'):
        img = img.convert('RGB')
    
    # 2. 缩小尺寸
    w, h = img.size
    if w > max_width:
        ratio = max_width / w
        img = img.resize((max_width, int(h * ratio)), Image.LANCZOS)
    
    # 3. 压缩保存
    img.save(output_path, 'JPEG', optimize=True, quality=quality)
    
    original = os.path.getsize(input_path) / 1024
    compressed = os.path.getsize(output_path) / 1024
    print(f"{os.path.basename(input_path)}: {original:.0f}KB → {compressed:.0f}KB")

# 使用
smart_compress("large_photo.jpg", "small_photo.jpg")

图片压缩的核心就一句话:先缩尺寸,再调质量,能转格式就转格式。掌握这三步,90% 的场景都够用了。

到此这篇关于从入门到实战详解Python压缩图片的完整方法的文章就介绍到这了,更多相关Python压缩图片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文