from typing import Literal, Annotated
from typer import Option, Typer
from pathlib import Path
from PIL import Image

app = Typer()

@app.command()
def main(file: Path,
         output_dir: Annotated[Path, Option("--output-dir", "-o")] = "./",
         output_name: Annotated[str, Option("--output-name", "-n")] = None,
         scale: Annotated[Literal["128", "64", "32", "16"], Option("--scale", "-s")] = "64"):
    name = file.stem
    output_dir.mkdir(parents=True, exist_ok=True)
    output_name = output_name or name
    scale = int(scale)
    with Image.open(file) as img:
        img = img.resize((scale, scale))  # 如果先缩放，后面会锁定长宽比
        img.save(f"{output_dir}/{output_name}.ico", format="ICO", sizes=[(scale, scale)])


if __name__ == '__main__':
    app()