#!/usr/bin/env python3
"""
Generate favicon.ico from SVG using cairosvg and PIL
"""

import os
import io
from cairosvg import svg2png
from PIL import Image

# Verzeichnis dieses Skripts
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))

def svg_to_ico(svg_path, ico_path, sizes=[16, 32, 48, 64, 128, 256]):
    """Convert SVG to multi-size ICO file"""
    img_io = io.BytesIO()
    
    # Höchste Auflösung für Zwischenspeicherung
    max_size = max(sizes)
    
    # SVG in PNG konvertieren
    with open(svg_path, 'rb') as svg_file:
        svg_data = svg_file.read()
        svg2png(bytestring=svg_data, write_to=img_io, output_width=max_size, output_height=max_size)
    
    # PNG in verschiedene Größen konvertieren
    img = Image.open(img_io)
    
    # Alle Größen für das ICO-Format vorbereiten
    img_list = []
    for size in sizes:
        resized_img = img.resize((size, size), Image.LANCZOS)
        img_list.append(resized_img)
    
    # ICO-Datei speichern
    img_list[0].save(
        ico_path,
        format='ICO',
        sizes=[(img.width, img.height) for img in img_list],
        append_images=img_list[1:]
    )
    print(f"Favicon {ico_path} wurde erstellt!")

# Ursprüngliches Favicon konvertieren
svg_to_ico(
    os.path.join(CURRENT_DIR, 'favicon.svg'),
    os.path.join(CURRENT_DIR, 'favicon.ico')
)

# Neues Neuron-Favicon konvertieren
svg_to_ico(
    os.path.join(CURRENT_DIR, 'neuron-favicon.svg'),
    os.path.join(CURRENT_DIR, 'neuron-favicon.ico')
) 