-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_png_icons.py
More file actions
83 lines (71 loc) · 2.67 KB
/
create_png_icons.py
File metadata and controls
83 lines (71 loc) · 2.67 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
#!/usr/bin/env python3
"""
Script to convert SVG icons to PNG for PWA
"""
import os
import subprocess
import sys
def create_png_from_svg(svg_path, png_path, size):
"""Convert SVG to PNG using cairosvg or rsvg-convert"""
try:
# Try using cairosvg first (Python library)
import cairosvg
cairosvg.svg2png(url=svg_path, write_to=png_path, output_width=size, output_height=size)
print(f"✅ Created {png_path} using cairosvg")
return True
except ImportError:
try:
# Try using rsvg-convert (system command)
subprocess.run([
'rsvg-convert',
'-w', str(size),
'-h', str(size),
'-o', png_path,
svg_path
], check=True)
print(f"✅ Created {png_path} using rsvg-convert")
return True
except (subprocess.CalledProcessError, FileNotFoundError):
try:
# Try using ImageMagick
subprocess.run([
'convert',
'-background', 'none',
'-size', f'{size}x{size}',
svg_path,
png_path
], check=True)
print(f"✅ Created {png_path} using ImageMagick")
return True
except (subprocess.CalledProcessError, FileNotFoundError):
print(f"❌ Failed to create {png_path}")
print("Please install one of: cairosvg, rsvg-convert, or ImageMagick")
return False
def main():
icons_dir = "static/icons"
# Check if icons directory exists
if not os.path.exists(icons_dir):
print(f"❌ Icons directory {icons_dir} not found")
return
# SVG files to convert
svg_files = [
("icon-192.svg", "icon-192.png", 192),
("icon-512.svg", "icon-512.png", 512)
]
success_count = 0
for svg_file, png_file, size in svg_files:
svg_path = os.path.join(icons_dir, svg_file)
png_path = os.path.join(icons_dir, png_file)
if not os.path.exists(svg_path):
print(f"❌ SVG file {svg_path} not found")
continue
if create_png_from_svg(svg_path, png_path, size):
success_count += 1
print(f"\n📊 Summary: {success_count}/{len(svg_files)} PNG icons created")
if success_count == len(svg_files):
print("🎉 All PNG icons created successfully!")
print("You can now install the PWA in Chrome")
else:
print("⚠️ Some icons failed to create. PWA installation may not work properly.")
if __name__ == "__main__":
main()