图片压缩代码
以下是Python中使用PIL库实现图片压缩的代码:
from PIL import Image
def compress_image(infile, outfile, quality):
"""
:param infile: 原始图片路径
:param outfile: 压缩后图片路径
:param quality: 压缩质量,取值范围为0-100
"""
with Image.open(infile) as im:
im.save(outfile, quality=quality)
使用方法:
compress_image("example.jpg", "example_compressed.jpg", 50)
其中,example.jpg为原始图片路径,example_compressed.jpg为压缩后图片路径,50为压缩质量,取值范围为0-100,数值越小压缩程度越大。
原文地址: https://www.cveoy.top/t/topic/e9Wx 著作权归作者所有。请勿转载和采集!