# -*- coding: utf-8 -*- """ @创建时间: 2026-05-16 @更新时间: 2026-05-16 @名称: resize_icon.py @作用: 将源图片裁剪为从16x16到1024x1024的常用尺寸图标 @上次更新内容: 初始创建 """ import os from PIL import Image SOURCE_IMAGE = os.path.join(os.path.dirname(__file__), "..", "assets", "templates", "xianyan.png") OUTPUT_DIR = os.path.join(os.path.dirname(__file__), "..", "assets", "templates", "resized") SIZES = [ 16, 20, 24, 29, 32, 40, 48, 50, 57, 58, 60, 64, 72, 76, 80, 87, 88, 96, 100, 114, 120, 128, 144, 152, 167, 180, 192, 216, 256, 512, 1024, ] SIZE_CATEGORIES = { "favicon": [16, 32], "ios_notification": [20], "ios_settings": [29], "ios_spotlight": [40], "ios_app": [60, 76, 87, 120, 152, 167, 180, 1024], "android_launcher": [48, 72, 96, 144, 192, 216], "web_app": [64, 128, 256, 512], "general": [24, 50, 57, 58, 80, 88, 100, 114], } def resize_image(source_path: str, output_dir: str, size: int) -> str: img = Image.open(source_path) if img.mode != "RGBA": img = img.convert("RGBA") resized = img.resize((size, size), Image.LANCZOS) filename = f"icon_{size}x{size}.png" filepath = os.path.join(output_dir, filename) resized.save(filepath, "PNG", compress_level=0) return filename def main(): source_path = os.path.normpath(SOURCE_IMAGE) output_dir = os.path.normpath(OUTPUT_DIR) if not os.path.exists(source_path): print(f"[ERROR] Source image not found: {source_path}") return os.makedirs(output_dir, exist_ok=True) print(f"Source: {source_path}") print(f"Output: {output_dir}") print(f"Sizes: {len(SIZES)} variants\n") for size in SIZES: filename = resize_image(source_path, output_dir, size) print(f" [OK] {filename}") print(f"\nDone! {len(SIZES)} icons generated in: {output_dir}") print("\n--- Size Category Reference ---") for category, sizes in SIZE_CATEGORIES.items(): matching = [s for s in sizes if s in SIZES] if matching: print(f" {category}: {', '.join(f'{s}x{s}' for s in matching)}") if __name__ == "__main__": main()