aboutsummaryrefslogtreecommitdiff
path: root/bin/migrate/2-compress-covers
blob: 0398689f75909d3a3cfb77696ff3f21e15d7c0fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)