-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_icns.py
More file actions
64 lines (53 loc) · 2.03 KB
/
Copy pathmake_icns.py
File metadata and controls
64 lines (53 loc) · 2.03 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
# mypy: ignore-errors
"""
Render src/photo_tagger/resources/icon.svg into a macOS ``icon.icns`` for the app bundle.
Uses Qt (already a GUI build dependency) to rasterize the SVG at the standard iconset sizes, then
``iconutil`` to pack them. Best-effort: build_macos_app.sh treats a missing icon.icns as "use the
default icon", so a failure here never blocks the build.
"""
import os
import subprocess
import sys
from pathlib import Path
# A complete macOS iconset: (pixel size, iconutil-required filename suffix).
_ICONSET = (
(16, "16x16"),
(32, "16x16@2x"),
(32, "32x32"),
(64, "32x32@2x"),
(128, "128x128"),
(256, "128x128@2x"),
(256, "256x256"),
(512, "256x256@2x"),
(512, "512x512"),
(1024, "512x512@2x"),
)
def main() -> int:
"""Rasterize the SVG into an iconset and pack it into icon.icns; return a process exit code."""
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") # render without a display
from PySide6.QtCore import Qt
from PySide6.QtGui import QImage, QPainter
from PySide6.QtSvg import QSvgRenderer
from PySide6.QtWidgets import QApplication
here = Path(__file__).resolve().parent
svg = here.parent / "src" / "photo_tagger" / "resources" / "icon.svg"
if not svg.is_file():
sys.stderr.write(f"icon source not found: {svg}\n")
return 1
QApplication([]) # QImage/QPainter need a Q*Application instance
renderer = QSvgRenderer(str(svg))
iconset = here / "icon.iconset"
iconset.mkdir(parents=True, exist_ok=True)
for size, suffix in _ICONSET:
image = QImage(size, size, QImage.Format.Format_ARGB32)
image.fill(Qt.GlobalColor.transparent)
painter = QPainter(image)
renderer.render(painter)
painter.end()
image.save(str(iconset / f"icon_{suffix}.png"), "PNG")
icns = here / "icon.icns"
subprocess.run(["/usr/bin/iconutil", "-c", "icns", str(iconset), "-o", str(icns)], check=True)
sys.stdout.write(f"wrote {icns}\n")
return 0
if __name__ == "__main__":
sys.exit(main())