-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.py
More file actions
124 lines (97 loc) · 3.73 KB
/
Copy pathexport.py
File metadata and controls
124 lines (97 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import sys
from PIL import Image
# ========= 配置 ========= #
SVG_FILE = os.path.join("svg", "LOGO.svg")
OUTPUT_ROOT = "exports"
SUBFOLDERS = {
"transparent": "png_transparent",
"white": "png_white",
"ico": "ico_single"
}
# PNG 输出尺寸(包括超大母图)
SIZES = [8192, 4096, 3072, 2048, 1536, 1024, 768, 512, 400, 300, 200, 128, 100, 64, 48, 32, 24, 20, 16]
# 单独导出的 ICO 尺寸
ICO_SIZES = [16, 20, 24, 28, 32, 40, 48, 64, 72, 80, 96, 128, 160, 192, 256, 384, 512, 768, 1024]
# ======================== #
# -------- CairoSVG 检测 -------- #
def try_import_cairosvg():
try:
import cairosvg
return cairosvg
except Exception:
print("❌ 未检测到 CairoSVG 或底层 cairo 库。")
print("\n💡 安装方法:")
if sys.platform == "darwin":
print(" brew install cairo pango gdk-pixbuf libffi")
elif sys.platform.startswith("linux"):
print(" sudo apt install libcairo2 libpango-1.0-0 libgdk-pixbuf2.0-0")
elif sys.platform.startswith("win"):
print(" pip install pycairo cairosvg (Windows 自动包含依赖)")
sys.exit(1)
# -------- 文件操作 -------- #
def ensure_dir(path):
os.makedirs(path, exist_ok=True)
def make_export_dirs():
"""为不同类型文件创建子目录"""
for folder in SUBFOLDERS.values():
ensure_dir(os.path.join(OUTPUT_ROOT, folder))
# -------- 导出函数 -------- #
def export_png(cairosvg, size, background="transparent"):
subdir = SUBFOLDERS["white" if background == "white" else "transparent"]
out_name = f"logo_{size}_{background}.png"
out_path = os.path.join(OUTPUT_ROOT, subdir, out_name)
# 透明底导出
if background == "transparent":
cairosvg.svg2png(
url=SVG_FILE,
write_to=out_path,
output_width=size,
output_height=size,
)
# 白底导出
else:
temp_path = out_path.replace(".png", "_temp.png")
cairosvg.svg2png(
url=SVG_FILE,
write_to=temp_path,
output_width=size,
output_height=size,
)
img = Image.open(temp_path).convert("RGBA")
white_bg = Image.new("RGBA", img.size, (255, 255, 255, 255))
white_bg.paste(img, mask=img.split()[3])
white_bg.convert("RGB").save(out_path)
os.remove(temp_path)
print(f"✅ 导出 {out_name}")
def export_single_icos():
"""为每个尺寸导出独立 ICO 文件"""
ico_dir = os.path.join(OUTPUT_ROOT, SUBFOLDERS["ico"])
ensure_dir(ico_dir)
# 从 8192 透明 PNG 缩放
base_img_path = os.path.join(OUTPUT_ROOT, SUBFOLDERS["transparent"], "logo_8192_transparent.png")
if not os.path.exists(base_img_path):
raise FileNotFoundError("未找到 8192px 透明底 PNG,请先导出 PNG。")
base = Image.open(base_img_path).convert("RGBA")
for s in ICO_SIZES:
resized = base.resize((s, s), Image.LANCZOS)
out_path = os.path.join(ico_dir, f"logo_{s}.ico")
resized.save(out_path, format="ICO", sizes=[(s, s)])
print(f"🪟 导出 ICO: {out_path}")
print(f"🎯 共导出 {len(ICO_SIZES)} 个单尺寸 ICO 文件。")
# -------- 主流程 -------- #
def main():
make_export_dirs()
cairosvg = try_import_cairosvg()
if not os.path.exists(SVG_FILE):
print(f"❌ 找不到源文件:{SVG_FILE}")
return
print(f"📥 渲染 SVG: {SVG_FILE}\n")
for size in SIZES:
export_png(cairosvg, size, "transparent")
export_png(cairosvg, size, "white")
export_single_icos()
print("\n🎉 所有尺寸导出完成!")
print(f"📁 输出路径:{os.path.abspath(OUTPUT_ROOT)}")
if __name__ == "__main__":
main()