#!/usr/bin/env nix-shell #!nix-shell -i python3 -p python3Packages.pillow import PIL.Image import glob import os import sys if len(sys.argv) == 2 and os.path.exists(sys.argv[1]): book_directory = sys.argv[1] else: print(f'Usage: {sys.argv[0]} book-directory') exit(1) def compress(path): directory = os.path.dirname(os.path.realpath(path)) image = PIL.Image.open(path) width, height = image.size if width > 300: image = image.resize((300, int(300 * height / width)), PIL.Image.LANCZOS) image = image.convert('RGB') image.save(f'{directory}/tmp.webp', 'WEBP', optimize=True, quality=85) os.remove(path) os.rename(f'{directory}/tmp.webp', f'{directory}/cover.webp') for path in glob.glob(f'{book_directory}/**/cover.*', recursive=True): compress(path)