diff options
author | Joris | 2023-09-16 18:26:32 +0200 |
---|---|---|
committer | Joris | 2023-09-16 18:26:32 +0200 |
commit | 230f2e0623d22af6e68466884bd5dea5dcb846dc (patch) | |
tree | 653e4e74334ebeb10228fac247e2984b51010331 /bin | |
parent | d070b208ed7512317f831a0921760f31fe19254b (diff) |
Add compression covers migration
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/migrate/2-compress-covers | 27 |
1 files changed, 27 insertions, 0 deletions
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) |