import subprocess
import os
from PIL import Image

svg_code = """<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 512 512">
  <rect x="56" y="56" width="400" height="400" rx="60" fill="#1e4d7a" stroke="#16324a" stroke-width="16"/>
  <circle cx="256" cy="256" r="120" fill="#d4a359" stroke="#ba8b38" stroke-width="12"/>
</svg>"""

html_code = f"""<!DOCTYPE html>
<html><head><style>
  body {{ margin: 0; padding: 0; background: transparent; overflow: hidden; }}
  svg {{ display: block; width: 512px; height: 512px; }}
</style></head><body>{svg_code}</body></html>"""

os.makedirs("scratch", exist_ok=True)
with open("scratch/test_icon.html", "w", encoding="utf-8") as f:
    f.write(html_code)

chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
abs_html = os.path.abspath("scratch/test_icon.html")
abs_out = os.path.abspath("scratch/test_icon.png")

cmd = [
    chrome_path,
    "--headless=new",
    "--disable-gpu",
    "--default-background-color=00000000",
    "--window-size=512,512",
    f"--screenshot={abs_out}",
    f"file:///{abs_html}"
]
subprocess.run(cmd, check=True)

im = Image.open(abs_out)
print(f"Rendered successfully! Size: {im.size}, Mode: {im.mode}")
im42 = im.resize((42, 42), Image.Resampling.LANCZOS)
im42.save("scratch/test_icon_42.png")
print("42x42 saved successfully!")
