#!/usr/bin/env python3 """将 PNG 图标转换为 ICO 格式""" from pathlib import Path def convert_png_to_ico(): """将 icon.png 转换为 icon.ico""" try: from PIL import Image except ImportError: print("需要安装 Pillow: pip install Pillow") return False # 优先使用运行目录下的 icon 目录 cwd_icon = Path.cwd() / 'icon' if (cwd_icon / 'icon.png').exists(): icon_dir = cwd_icon elif (Path.cwd() / 'src' / 'icon' / 'icon.png').exists(): icon_dir = Path.cwd() / 'src' / 'icon' else: icon_dir = Path(__file__).parent png_path = icon_dir / 'icon.png' ico_path = icon_dir / 'icon.ico' if not png_path.exists(): print(f"找不到 PNG 文件: {png_path}") return False try: # 打开 PNG 图像 img = Image.open(png_path) # 转换为 RGBA 模式(如果不是的话) if img.mode != 'RGBA': img = img.convert('RGBA') # 创建多尺寸的 ICO 文件 sizes = [(16, 16), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)] icons = [] for size in sizes: resized = img.resize(size, Image.Resampling.LANCZOS) icons.append(resized) # 保存为 ICO icons[0].save(ico_path, format='ICO', sizes=[(s[0], s[1]) for s in sizes]) print(f"已生成 ICO 文件: {ico_path}") return True except Exception as e: print(f"转换失败: {e}") return False if __name__ == '__main__': convert_png_to_ico()