Skip to content

Commit 01f51d9

Browse files
committed
complete
1 parent f0aca02 commit 01f51d9

9 files changed

+135
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
# svg2ico
22
convert svg icon to ico
3+
4+
5+
6+
比较SVG和ICO
7+
8+
| **名称** | [**SVG**](https://www.aconvert.com/cn/format/svg/) | [**ICO**](https://www.aconvert.com/cn/format/ico/) |
9+
| :----------- | ------------------------------------------------------------ | :----------------------------------------------------------- |
10+
| **全名** | Scalable Vector Graphics | Microsoft icon |
11+
| **扩展名** | .svg, .svgz | .ico |
12+
| **MIME** | image/svg+xml | image/x-icon, image/vnd.-microsoft.icon |
13+
| **开发商** | W3C | 微软 |
14+
| **类型** | 矢量图形格式 | 图标文件格式 |
15+
| **介绍** | 可缩放矢量图形是基于可扩展标记语言(标准通用标记语言的子集),用于描述二维矢量图形的一种图形格式。它由万维网联盟制定,是一个开放标准。由于SVG是XML文件,SVG图像可以用任何文本编辑器创建,但它往往是与一个绘图程序一起使用,如Inkscape,更方便地创建SVG图像。 | ICO是Windows的图标文件格式,图标文件可以存储单个图案、多尺寸、多色板的图标文件。图标有一套标准的大小和属性格式,且通常是小尺寸的。每个图标都含有多张相同显示内容的图片,每一张图片具有不同的尺寸和发色数。一个图标就是一套相似的图片,每一张图片有不同的格式。从这一点上说图标是三维的。 |
16+
| **相关程序** | Mozilla Firefox, Internet Explorer, Google Chrome, Opera, Safari | Axialis IconWorkshop, IcoFX, IconBuilder, Microangelo Toolset, Greenfish Icon Editor Pro, GIMP, ImageMagick, IrfanView, ResEdit. |
17+
| **示例文件** | [sample.svg](https://www.aconvert.com/samples/sample.svg) | [sample.ico](https://www.aconvert.com/samples/sample.ico) |
18+
| **百科** | [SVG百科](https://baike.baidu.com/item/SVG/63178) | [ICO百科](https://baike.baidu.com/item/ICO/2868562) |
19+
20+
[Source](https://www.aconvert.com/cn/icon/svg-to-ico/)

assets/logo-128.ico

29.1 KB
Binary file not shown.

assets/logo-16.ico

925 Bytes
Binary file not shown.

assets/logo-256.ico

66.5 KB
Binary file not shown.

assets/logo-32.ico

5.04 KB
Binary file not shown.

assets/logo-64.ico

15.4 KB
Binary file not shown.

assets/logo.ico

5.04 KB
Binary file not shown.

assets/logo.svg

+1
Loading

svg2ico.ipynb

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 24,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# %pip install cairosvg pillow"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 25,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import cairosvg\n",
19+
"from PIL import Image\n",
20+
"import os\n",
21+
"import uuid\n",
22+
"\n",
23+
"\n",
24+
"def svg_to_ico(svg_file_path, ico_file_path, size=(32, 32)):\n",
25+
" # 生成唯一的临时文件名\n",
26+
" png_file_path = f\"temp_{uuid.uuid4().hex}.png\"\n",
27+
" cairosvg.svg2png(url=svg_file_path, write_to=png_file_path)\n",
28+
"\n",
29+
" # 打开 PNG 文件并调整大小\n",
30+
" img = Image.open(png_file_path)\n",
31+
" img = img.resize(size, Image.Resampling.LANCZOS)\n",
32+
"\n",
33+
" # 保存为 ICO 文件\n",
34+
" img.save(ico_file_path, format=\"ICO\")\n",
35+
"\n",
36+
" # 删除临时 PNG 文件\n",
37+
" os.remove(png_file_path)\n",
38+
"\n",
39+
" print(f\"{size} ICO 文件已生成!\")"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 26,
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"name": "stdout",
49+
"output_type": "stream",
50+
"text": [
51+
"(64, 64) ICO 文件已生成!\n"
52+
]
53+
}
54+
],
55+
"source": [
56+
"# 示例用法\n",
57+
"svg_to_ico(\"./assets/logo.svg\", \"./assets/logo-64.ico\", size=(64, 64))"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 27,
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"name": "stdout",
67+
"output_type": "stream",
68+
"text": [
69+
"(2, 2) ICO 文件已生成!\n",
70+
"(4, 4) ICO 文件已生成!\n",
71+
"(8, 8) ICO 文件已生成!\n",
72+
"(16, 16) ICO 文件已生成!\n",
73+
"(32, 32) ICO 文件已生成!\n",
74+
"(64, 64) ICO 文件已生成!\n",
75+
"(128, 128) ICO 文件已生成!\n",
76+
"(256, 256) ICO 文件已生成!\n"
77+
]
78+
}
79+
],
80+
"source": [
81+
"# 批量生成 ico 图标\n",
82+
"for i in range(1,9):\n",
83+
" size = 2**i\n",
84+
" svg_to_ico(\"./assets/logo.svg\", f\"./assets/logo-{size}.ico\", size=(size, size))"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {},
91+
"outputs": [],
92+
"source": []
93+
}
94+
],
95+
"metadata": {
96+
"kernelspec": {
97+
"display_name": "dash",
98+
"language": "python",
99+
"name": "python3"
100+
},
101+
"language_info": {
102+
"codemirror_mode": {
103+
"name": "ipython",
104+
"version": 3
105+
},
106+
"file_extension": ".py",
107+
"mimetype": "text/x-python",
108+
"name": "python",
109+
"nbconvert_exporter": "python",
110+
"pygments_lexer": "ipython3",
111+
"version": "3.9.19"
112+
}
113+
},
114+
"nbformat": 4,
115+
"nbformat_minor": 2
116+
}

0 commit comments

Comments
 (0)