from PIL import Image import os # 源图片路径 source_path = r"e:\project\flutter\f3\flutter_application_2\assets\app_icon_square.png" web_dir = r"e:\project\flutter\f3\flutter_application_2\web" # 打开源图片 img = Image.open(source_path) print(f"源图片尺寸: {img.size}") # 替换的图标列表 icons = [ # (文件名, 尺寸, 是否是maskable) ("favicon.png", (32, 32), False), ("icons/Icon-192.png", (192, 192), False), ("icons/Icon-512.png", (512, 512), False), ("icons/Icon-maskable-192.png", (192, 192), True), ("icons/Icon-maskable-512.png", (512, 512), True), ] for filename, size, is_maskable in icons: output_path = os.path.join(web_dir, filename) # 调整尺寸 resized = img.resize(size, Image.Resampling.LANCZOS) # 如果是 maskable 图标,可以添加一些边距(可选) if is_maskable: # 创建一个更大的画布,添加边距 margin = int(size[0] * 0.1) # 10% 边距 new_size = (size[0] + margin * 2, size[1] + margin * 2) background = Image.new("RGBA", new_size, (0, 0, 0, 0)) background.paste(resized, (margin, margin)) resized = background.resize(size, Image.Resampling.LANCZOS) # 保存 resized.save(output_path, "PNG") print(f"替换: {output_path} ({size})") print("\n所有 web 图标替换完成!")