Download tools/normalize_cards.py from Lashtw/idle: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/spaces/Lashtw/idle/resolve/main/tools/normalize_cards.py
- Command line
-
hf download hf://spaces/Lashtw/idle/tools/normalize_cards.py
-
curl -L -o normalize_cards.py https://huggingface.co/spaces/Lashtw/idle/resolve/main/tools/normalize_cards.py
2.25 kB
| """Normalize raw TCG card images to 896x1200 JPG and WebP in assets/cards/.""" | |
| import os | |
| import shutil | |
| from pathlib import Path | |
| from PIL import Image | |
| NAME_MAP = { | |
| '亞歷山大': 'alexander', | |
| '伊麗莎白': 'elizabeth', | |
| '岳飛': 'yuefei', | |
| '劉銘傳': 'liumingchuan', | |
| '李鴻章': 'lihongzhang', | |
| '戚繼光': 'qijiguang', | |
| '施琅': 'shilang', | |
| '樺山資紀': 'kabayamasukenori', | |
| '趙匡胤': 'zhaokuangyin', | |
| '秋瑾': 'qiujin', | |
| '鄭成功': 'zhengchenggong', | |
| '劉永福': 'liuyongfu', | |
| '拿破崙': 'napoleon', | |
| '俾斯麥': 'bismarck', | |
| '貞德': 'jeanne', | |
| } | |
| TIERS = ['SSR', 'SR', 'R', 'C'] | |
| def normalize_one(src_path: Path, hero_id: str, tier: str, out_dir: Path): | |
| """Resize src_path to 896x1200 and save JPG + WebP.""" | |
| im = Image.open(src_path).convert('RGB') | |
| if im.size != (896, 1200): | |
| im = im.resize((896, 1200), Image.Resampling.LANCZOS) | |
| base = f'tcg_{hero_id}_{tier.lower()}' | |
| jpg_out = out_dir / f'{base}.jpg' | |
| webp_out = out_dir / f'{base}.webp' | |
| im.save(jpg_out, format='JPEG', quality=92, optimize=True) | |
| im.save(webp_out, format='WEBP', quality=85, method=6) | |
| print(f'Normalized {src_path.name} -> {jpg_out.name} ({jpg_out.stat().st_size // 1024}KB), {webp_out.name} ({webp_out.stat().st_size // 1024}KB)') | |
| def main(): | |
| raw_dir = Path('assets/cards/1') | |
| out_dir = Path('assets/cards') | |
| done_dir = Path('assets/cards/_raw_done') | |
| if not raw_dir.exists(): | |
| print('No raw_dir found.') | |
| return | |
| done_dir.mkdir(parents=True, exist_ok=True) | |
| for f in sorted(raw_dir.iterdir()): | |
| if not f.is_file() or f.suffix.lower() not in ('.jpg', '.jpeg', '.png', '.webp'): | |
| continue | |
| stem = f.stem | |
| matched = False | |
| for zh, hid in NAME_MAP.items(): | |
| if stem.startswith(zh): | |
| rest = stem[len(zh):].upper() | |
| if rest in TIERS: | |
| normalize_one(f, hid, rest, out_dir) | |
| shutil.move(str(f), str(done_dir / f.name)) | |
| matched = True | |
| break | |
| if not matched: | |
| print(f'Skipped unrecognized filename: {f.name}') | |
| if __name__ == '__main__': | |
| main() | |