"""Create web-sized JPEG copies of the full-size strips in cartoons/ under cartoons/web/.

Requires Pillow (not a runtime dependency of the site). Usage:
    python tools/make_web_strips.py [--width 1200] [--force]
"""
import argparse
from pathlib import Path

from PIL import Image

CARTOONS = Path(__file__).resolve().parent.parent / 'cartoons'


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--width', type=int, default=1200)
    parser.add_argument('--force', action='store_true')
    args = parser.parse_args()

    out_dir = CARTOONS / 'web'
    out_dir.mkdir(exist_ok=True)
    for src in sorted(CARTOONS.glob('The_DR_Is_In_*.png')):
        out = out_dir / f'{src.stem}.jpg'
        if out.exists() and not args.force:
            continue
        with Image.open(src) as img:
            width = min(args.width, img.width)
            height = round(img.height * width / img.width)
            resized = img.convert('RGB').resize((width, height), Image.LANCZOS)
            resized.save(out, 'JPEG', quality=88, optimize=True)
        print(f'Created {out} ({width}x{height})')


if __name__ == '__main__':
    main()
