40 lines
1.8 KiB
Python
40 lines
1.8 KiB
Python
"""從 icon/ 裡的圖片產生網頁用的圖:
|
||
|
||
server/.venv/Scripts/python tools/make_icons.py # 用 icon/ 裡的第一張圖當圖標
|
||
server/.venv/Scripts/python tools/make_icons.py icon/x.png
|
||
|
||
另外把 icon/checkin/0.png、1.png(簽到按鈕按下前/後)縮成 web/media/checkin-0.webp、checkin-1.webp。
|
||
"""
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
from PIL import Image, ImageOps
|
||
|
||
ROOT = Path(__file__).resolve().parent.parent
|
||
WEB = ROOT / "web"
|
||
IMG_EXTS = {".png", ".jpg", ".jpeg", ".webp"}
|
||
|
||
src = Path(sys.argv[1]) if len(sys.argv) > 1 else next(
|
||
p for p in sorted((ROOT / "icon").iterdir()) if p.is_file() and p.suffix.lower() in IMG_EXTS)
|
||
with Image.open(src) as im:
|
||
im = ImageOps.exif_transpose(im).convert("RGB")
|
||
im = ImageOps.fit(im, (512, 512), Image.LANCZOS) # 不是正方形時置中裁切
|
||
|
||
im.save(WEB / "favicon.ico", sizes=[(16, 16), (32, 32), (48, 48)])
|
||
for name, size in {"icon-192.png": 192, "icon-512.png": 512, "apple-touch-icon.png": 180, "logo.png": 64}.items():
|
||
im.resize((size, size), Image.LANCZOS).save(WEB / name, optimize=True)
|
||
print(f"{src.name} -> web/favicon.ico, icon-192.png, icon-512.png, apple-touch-icon.png, logo.png")
|
||
|
||
# 簽到按鈕的兩張圖:原圖 1280×1280 約 1 MB,縮成 512×512 WebP,手機載入才快
|
||
faces = ROOT / "icon" / "checkin"
|
||
(WEB / "media").mkdir(exist_ok=True)
|
||
for n in ("0", "1"):
|
||
p = next((faces / f"{n}{ext}" for ext in IMG_EXTS if (faces / f"{n}{ext}").exists()), None)
|
||
if p is None:
|
||
continue
|
||
with Image.open(p) as im:
|
||
im = ImageOps.fit(ImageOps.exif_transpose(im).convert("RGB"), (512, 512), Image.LANCZOS)
|
||
out = WEB / "media" / f"checkin-{n}.webp"
|
||
im.save(out, "WEBP", quality=85, method=6)
|
||
print(f"checkin/{p.name} -> web/media/{out.name} ({out.stat().st_size // 1024} KB)")
|