Image file compression

Image file compression. PIL library using the picture compression ratio, etc., regardless of pre-compressed file size, the compressed file is less than 10KB.

For example to nest

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')

Artwork
After treatment

Published an original article · won praise 1 · views 31

Guess you like

Origin blog.csdn.net/qq_45851194/article/details/104921357