[教程]使用WebP格式图片以节省空间占用并加上水印

写了个脚本,自动将文件夹内的图片转成WebP格式,并加上网址水印

import os
from PIL import Image

def convert_to_webp(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(('.png', '.jpg', '.jpeg')):
                file_path = os.path.join(root, file)
                image = Image.open(file_path)
                webp_path = file_path.rsplit('.', 1)[0] + '.webp'
                image.save(webp_path, 'webp')

convert_to_webp('.')
主要是为了给Cloudflare Pages中的静态文件减减肥,顺便加个水印