-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_favicons.py
More file actions
92 lines (79 loc) · 2.92 KB
/
Copy pathgenerate_favicons.py
File metadata and controls
92 lines (79 loc) · 2.92 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
import os
import cairosvg
from PIL import Image
import logging
logger = logging.getLogger(__name__)
def ensure_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def generate_favicons():
# Add error handling and logging
try:
static_dir = os.path.join(os.path.dirname(__file__), '..', 'static')
ensure_dir(static_dir)
# Check if source SVG exists
svg_path = os.path.join(static_dir, 'favicon.svg')
if not os.path.exists(svg_path):
logger.error(f"Source favicon.svg not found at {svg_path}")
# Generate a default favicon if SVG is missing
generate_default_favicon()
return
# Read the SVG content
svg_path = os.path.join(static_dir, 'favicon.svg')
# Generate PNG versions
sizes = {
'favicon-16x16.png': 16,
'favicon-32x32.png': 32,
'favicon-192x192.png': 192,
'favicon-512x512.png': 512,
'apple-touch-icon.png': 180
}
for filename, size in sizes.items():
output_path = os.path.join(static_dir, filename)
cairosvg.svg2png(
url=svg_path,
write_to=output_path,
output_width=size,
output_height=size
)
# Create ICO file
ico_path = os.path.join(static_dir, 'favicon.ico')
img = Image.open(os.path.join(static_dir, 'favicon-32x32.png'))
img.save(ico_path)
# Create web manifest
manifest = {
"name": "MultiLLM Proxy",
"short_name": "MultiLLM",
"icons": [
{
"src": "/static/favicon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/static/favicon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"theme_color": "#4F46E5",
"background_color": "#ffffff",
"display": "standalone"
}
import json
with open(os.path.join(static_dir, 'site.webmanifest'), 'w') as f:
json.dump(manifest, f, indent=2)
except Exception as e:
logger.error(f"Error generating favicons: {str(e)}")
# Generate a default favicon on error
generate_default_favicon()
def generate_default_favicon():
"""Generate a simple default favicon if the SVG source is missing"""
from PIL import Image
static_dir = os.path.join(os.path.dirname(__file__), '..', 'static')
ensure_dir(static_dir)
# Create a 32x32 black square as default favicon
img = Image.new('RGB', (32, 32), color='black')
img.save(os.path.join(static_dir, 'favicon.ico'))
if __name__ == '__main__':
generate_favicons()