| Server IP : 3.147.158.171 / Your IP : 216.73.216.88 Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.21 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /tsai/repo/builder/backend/scripts/ |
Upload File : |
#!/usr/bin/env python3
"""
Generate SVG Favicon
Generates an SVG favicon with a rounded rectangle background and centered text.
Uses Material 3 color palette.
Usage:
python generate_favicon.py <char> <bg_color> <fg_color> [output_file]
Arguments:
char Character(s) to display (typically 1-2 letters)
bg_color Background color name (e.g., 'azure', 'blue') or hex value
fg_color Foreground/text color name or hex value
output_file Output file path (default: favicon.svg)
Examples:
python generate_favicon.py T azure white
python generate_favicon.py AB blue white /tmp/favicon.svg
python generate_favicon.py X "#ff5500" "#ffffff"
"""
from __future__ import annotations
import sys
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
# Material 3 color palette
COLORS = {
"azure": "#0074e9",
"blue": "#5a64ff",
"violet": "#944aff",
"magenta": "#d200d2",
"rose": "#e80074",
"red": "#ef0000",
"orange": "#bc5d00",
"yellow": "#7b7b00",
"chartreuse": "#418700",
"green": "#038b00",
"spring-green": "#008942",
"cyan": "#008585",
"black": "#000000",
"white": "#ffffff",
"gray": "#6b7280",
}
def get_color_hex(color_name: str) -> str:
"""Convert color name to hex value, or return as-is if already hex."""
if color_name.startswith("#"):
return color_name
return COLORS.get(color_name.lower(), COLORS["azure"])
def generate_svg(char: str, bg_color: str, fg_color: str) -> str:
"""Generate SVG favicon content."""
bg_hex = get_color_hex(bg_color)
fg_hex = get_color_hex(fg_color)
# Adjust font size based on character count
font_size = 320 if len(char) == 1 else 200
svg = f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<rect x="0" y="0" width="512" height="512" rx="77" ry="77" fill="{bg_hex}"/>
<text x="256" y="256" font-family="system-ui, -apple-system, sans-serif" font-size="{font_size}" font-weight="600" fill="{fg_hex}" text-anchor="middle" dominant-baseline="central">{char}</text>
</svg>'''
return svg
FONT_PATHS = [
"/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf",
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
"/usr/share/fonts/google-noto/NotoSans-Bold.ttf",
"/usr/share/fonts/truetype/noto/NotoSans-Bold.ttf",
"/System/Library/Fonts/Helvetica.ttc",
]
def find_font(size: int) -> ImageFont.FreeTypeFont | ImageFont.ImageFont:
"""Find a system font, falling back to Pillow default."""
for path in FONT_PATHS:
if Path(path).exists():
return ImageFont.truetype(path, size)
return ImageFont.load_default(size)
def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:
"""Convert hex color string to RGB tuple."""
hex_color = hex_color.lstrip("#")
return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4))
def generate_png(char: str, bg_color: str, fg_color: str, output_path: Path):
"""Generate a 512x512 PNG favicon matching the SVG design."""
bg_rgb = hex_to_rgb(get_color_hex(bg_color))
fg_rgb = hex_to_rgb(get_color_hex(fg_color))
img = Image.new("RGBA", (512, 512), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
draw.rounded_rectangle([0, 0, 511, 511], radius=77, fill=bg_rgb)
font_size = 320 if len(char) == 1 else 200
font = find_font(font_size)
bbox = draw.textbbox((0, 0), char, font=font)
text_w = bbox[2] - bbox[0]
text_h = bbox[3] - bbox[1]
x = (512 - text_w) / 2 - bbox[0]
y = (512 - text_h) / 2 - bbox[1]
draw.text((x, y), char, fill=fg_rgb, font=font)
img.save(str(output_path), "PNG")
def main():
if len(sys.argv) < 4:
print(__doc__)
sys.exit(1)
char = sys.argv[1]
bg_color = sys.argv[2]
fg_color = sys.argv[3]
output_file = sys.argv[4] if len(sys.argv) > 4 else "favicon.svg"
svg_content = generate_svg(char, bg_color, fg_color)
output_path = Path(output_file)
output_path.write_text(svg_content)
print(f"Generated favicon: {output_path}")
# Also generate PNG for WordPress Site Icon registration
png_path = output_path.with_suffix(".png")
generate_png(char, bg_color, fg_color, png_path)
print(f"Generated PNG: {png_path}")
if __name__ == "__main__":
main()