图像文件压缩

图像文件压缩。使用PIL库对图片进行等比例压缩,无论压缩前文件大小如何,压缩后文件小于10KB。

以鸟巢为例

from PIL import Image
import os
import math
def getsize(path):
    return os.stat(path).st_size
def compress(path):
    size = getsize(path)
    ratio = math.sqrt(10*1024/size)
    im = Image.open(path)
    height = im.height;
    width = im.width;
    m_height = int(ratio*height)
    m_width = int(ratio*width)
    ph = im.resize((m_width,m_height))
    ph.save('test_compressed.jpg',)
compress('birdnest.jpg')

原图
在处理后

发布了1 篇原创文章 · 获赞 1 · 访问量 31

猜你喜欢

转载自blog.csdn.net/qq_45851194/article/details/104921357