全blog图片转为webp

2018 - 05 - 17

全 blog 从 png, jpg, jpeg 统一到 webp 使得整体图片文件大小从45M降到15M. 很好的实践 不错.

其中用到:

#! /usr/local/bin/python3.6  
import re  
import os  
import sys  

def totalFiles(path):  
    filesPath = []  
    for root, dirs, files in os.walk(path):  
        for file in files:  
            filesPath.append(os.path.join(root, file))  
    return filesPath  

def coverImage(files, output):  
    m = re.compile('(.+)\.(png|jpg|jpeg)$', flags=re.I)  
    for name in files:  
        res=m.search(name)  
        if res != None:  
            basename = os.path.basename(res.group(1) + '.webp')  
            os.system('cwebp "' + name + '" -o "' + os.path.join(output, basename) + '"')  

try:  
    path = sys.argv[1]  
except Exception as e:  
    path = os.curdir  

output = os.path.join(os.curdir, "Output")  
os.makedirs(output, exist_ok = True)  
files = totalFiles(path)  
coverImage(files, output)  
Table of Contents