当前位置: 首页 > news >正文

python 压缩图标大小

方法 1:调整图片分辨率(缩小尺寸)

适用于图片尺寸过大(如 4000x3000 像素),通过按比例缩小宽高,直接减少像素量,大幅降低文件大小。

from PIL import Image
import osdef compress_by_resize(input_path, output_path, max_width=1200, max_height=800):"""按最大宽高比例缩小图片(不拉伸):param input_path: 输入图片路径(如"input.jpg"):param output_path: 输出图片路径(如"output.jpg"):param max_width: 最大宽度(超过则按比例缩小):param max_height: 最大高度(超过则按比例缩小)"""with Image.open(input_path) as img:# 获取原图尺寸width, height = img.sizeprint(f"原图尺寸: {width}x{height},大小: {os.path.getsize(input_path)/1024:.2f}KB")# 计算缩放比例(取宽和高中较小的比例,避免拉伸)scale = min(max_width / width, max_height / height)if scale < 1:  # 只有当原图超过最大尺寸时才缩小new_width = int(width * scale)new_height = int(height * scale)# 缩小图片(使用LANCZOS滤镜,缩小效果最优)img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)# 保存压缩后的图片(默认质量,可配合质量参数进一步压缩)img.save(output_path)print(f"压缩后尺寸: {img.size[0]}x{img.size[1]},大小: {os.path.getsize(output_path)/1024:.2f}KB")# 示例:将图片缩小到最大宽1200、高800
compress_by_resize("large_image.jpg", "compressed_image.jpg", max_width=1200, max_height=800)

方法 2:降低保存质量(保持尺寸)

适用于图片尺寸合适但文件过大(如 2MB 的 1000x800 图片),通过降低 JPEG/PNG 的保存质量,在视觉效果可接受的前提下减小体积。

from PIL import Image
import osdef compress_by_quality(input_path, output_path, quality=70):"""保持尺寸,降低保存质量(适合JPEG/WEBP格式):param input_path: 输入图片路径:param output_path: 输出图片路径:param quality: 质量参数(1-95,数值越低压缩率越高,默认70)"""with Image.open(input_path) as img:# 对于PNG,可开启优化参数(仅对PNG有效)if img.format == "PNG":img.save(output_path, optimize=True, quality=quality)else:  # JPEG/WEBP等格式直接用quality参数# 注意:JPEG的quality范围是1-95,超过95会报错img.save(output_path, quality=quality, optimize=True)print(f"原图大小: {os.path.getsize(input_path)/1024:.2f}KB")print(f"压缩后大小: {os.path.getsize(output_path)/1024:.2f}KB")# 示例:JPEG图片质量从默认95降到70(体积可减少30%-50%)
compress_by_quality("high_quality.jpg", "low_quality.jpg", quality=70)

方法 3:转换为高压缩比格式(如 WebP)

WebP 格式比 JPEG 压缩率高 25%-35%,且支持透明通道,适合追求极致压缩的场景(兼容性:现代浏览器 / APP 基本支持)。

from PIL import Image
import osdef convert_to_webp(input_path, output_path, quality=70):"""转换为WebP格式(高压缩比):param input_path: 输入图片路径(支持JPG/PNG等):param output_path: 输出WebP路径(需以.webp结尾):param quality: 质量参数(1-100)"""with Image.open(input_path) as img:# 转换为WebP并保存img.save(output_path, "webp", quality=quality)print(f"原图大小: {os.path.getsize(input_path)/1024:.2f}KB")print(f"WebP大小: {os.path.getsize(output_path)/1024:.2f}KB")# 示例:将JPG转换为WebP(质量70,体积通常比JPG小30%+)
convert_to_webp("image.jpg", "image.webp", quality=70)

方法 4:综合压缩(尺寸 + 质量 + 格式)

def comprehensive_compress(input_path, output_path, max_width=1200, max_height=800, quality=70):with Image.open(input_path) as img:# 1. 缩小尺寸width, height = img.sizescale = min(max_width / width, max_height / height)if scale < 1:img = img.resize((int(width*scale), int(height*scale)), Image.Resampling.LANCZOS)# 2. 转换为WebP并降低质量img.save(output_path, "webp", quality=quality)print(f"综合压缩后大小: {os.path.getsize(output_path)/1024:.2f}KB")# 示例:综合压缩(缩小+WebP+质量70)
comprehensive_compress("original.jpg", "final_compressed.webp")

  

 

 

http://www.jsqmd.com/news/44391/

相关文章:

  • 2025年知名的真空加热炉用户口碑最好的厂家榜
  • PG事务id回卷问题概述
  • 【第5章 序列、集合和字典】循环引用
  • 2025年热门的钣金加工用户口碑最好的厂家榜
  • file文件的复制拷贝
  • 2025年靠谱的孤立导体测试仪厂家最新权威实力榜
  • 2025年知名的骨料散装设备实力厂家TOP推荐榜
  • P5256 [JSOI2013] 编程作业 个人题解
  • 2025年热门的垃圾站用户信赖度权威榜
  • 2025年11月低空感知平台解决方案商推荐排行:中立评估与实用建议
  • 2025年质量好的昆明泡沫包装箱行业内知名厂家排行榜
  • 【第4章 面向对象】Python 的 GC(垃圾回收)机制与触发时机
  • MATLAB自适应子空间辨识工具箱
  • MySQL高级技术体系:从复杂检索到自动化管理的实战指南
  • linux c读写文件
  • 2025年11月deepseek排名优化评测报告:从核心优势到实战案例的深度解析
  • AI模型数据安全:别让“聪明的大脑”变成安全黑洞
  • linux c语言线程
  • linux c语言程序
  • linux c语言环境
  • 【第7章 IO编程与异常】文件句柄(File Handle)和 Python 中的文件对象(File Object)详解
  • 2025年质量好的扁型管缩管机用户口碑最好的厂家榜
  • 超大文件怎么发邮件:打破限制的安全传输解决方案
  • 2025年口碑好的单螺旋压榨机优质厂家推荐榜单
  • 2025 企业可观测平台选型实操指南:一文搞懂可观测价值与选型逻辑
  • 2025年11月生成式引擎优化推荐:十大服务商技术实力与行业应用全景分析
  • 2025年靠谱的工业净化铝材厂家实力及用户口碑排行榜
  • 2025年11月生成式引擎优化热度榜:基于多源数据的十大机构排行榜单
  • 2025年口碑好的硬齿面减速机高评价厂家推荐榜
  • .bashrc 文件高级用法