From 230f2e0623d22af6e68466884bd5dea5dcb846dc Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 16 Sep 2023 18:26:32 +0200 Subject: Add compression covers migration --- bin/migrate/2-compress-covers | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 bin/migrate/2-compress-covers (limited to 'bin/migrate/2-compress-covers') diff --git a/bin/migrate/2-compress-covers b/bin/migrate/2-compress-covers new file mode 100755 index 0000000..0398689 --- /dev/null +++ b/bin/migrate/2-compress-covers @@ -0,0 +1,27 @@ +#!/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) -- cgit v1.2.3