"""Render mulberry/assets/mulberry.svg into PNG sizes and a Windows .ico.""" import sys from pathlib import Path from PyQt6.QtCore import QBuffer, QByteArray, QIODevice from PyQt6.QtGui import QGuiApplication, QImage, QPainter from PyQt6.QtSvg import QSvgRenderer ROOT = Path(__file__).resolve().parent.parent OUT = ROOT / "packaging" / "build-icons" def render(r, size): img = QImage(size, size, QImage.Format.Format_ARGB32) img.fill(0) p = QPainter(img) r.render(p) p.end() return img def main(): QGuiApplication(sys.argv[:1] + ["-platform", "offscreen"]) OUT.mkdir(parents=True, exist_ok=True) r = QSvgRenderer(str(ROOT / "mulberry" / "assets" / "mulberry.svg")) pngs = {} for s in (16, 24, 32, 48, 64, 128, 256, 512): img = render(r, s) img.save(str(OUT / f"mulberry-{s}.png")) ba = QByteArray() buf = QBuffer(ba) buf.open(QIODevice.OpenModeFlag.WriteOnly) img.save(buf, "PNG") pngs[s] = bytes(ba) # ICO with embedded PNGs (Vista+ format) sizes = [16, 24, 32, 48, 64, 128, 256] header = (0).to_bytes(2, "little") + (1).to_bytes(2, "little") + len(sizes).to_bytes(2, "little") entries, data = b"", b"" offset = 6 + 16 * len(sizes) for s in sizes: d = pngs[s] dim = 0 if s >= 256 else s entries += bytes([dim, dim, 0, 0]) + (1).to_bytes(2, "little") + (32).to_bytes(2, "little") \ + len(d).to_bytes(4, "little") + offset.to_bytes(4, "little") data += d offset += len(d) (OUT / "mulberry.ico").write_bytes(header + entries + data) print("icons written to", OUT) if __name__ == "__main__": main()