storeframe 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- storeframe/__init__.py +24 -0
- storeframe/__main__.py +5 -0
- storeframe/batch.py +87 -0
- storeframe/cli.py +108 -0
- storeframe/design.py +147 -0
- storeframe/devices.py +267 -0
- storeframe/fonts/Nunito[wght].ttf +0 -0
- storeframe/fonts/OFL.txt +93 -0
- storeframe/geometry.py +100 -0
- storeframe/layout.py +54 -0
- storeframe/paint.py +104 -0
- storeframe/platforms.py +57 -0
- storeframe/py.typed +0 -0
- storeframe/render.py +86 -0
- storeframe/styles/__init__.py +23 -0
- storeframe/styles/arc.py +67 -0
- storeframe/styles/base.py +72 -0
- storeframe/styles/tabs.py +205 -0
- storeframe/text.py +162 -0
- storeframe-0.1.0.dist-info/METADATA +132 -0
- storeframe-0.1.0.dist-info/RECORD +25 -0
- storeframe-0.1.0.dist-info/WHEEL +4 -0
- storeframe-0.1.0.dist-info/entry_points.txt +2 -0
- storeframe-0.1.0.dist-info/licenses/LICENSE +21 -0
- storeframe-0.1.0.dist-info/licenses/src/storeframe/fonts/OFL.txt +93 -0
storeframe/__init__.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""Frame app screenshots in drawn device bodies for the App Store and Google Play."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__version__ = "0.1.0"
|
|
6
|
+
|
|
7
|
+
from .batch import Report, frame_directory
|
|
8
|
+
from .design import Design, Screen
|
|
9
|
+
from .devices import DEVICES, Device
|
|
10
|
+
from .layout import Layout
|
|
11
|
+
from .render import feature_graphic, screenshot
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"DEVICES",
|
|
15
|
+
"Design",
|
|
16
|
+
"Device",
|
|
17
|
+
"Layout",
|
|
18
|
+
"Report",
|
|
19
|
+
"Screen",
|
|
20
|
+
"__version__",
|
|
21
|
+
"feature_graphic",
|
|
22
|
+
"frame_directory",
|
|
23
|
+
"screenshot",
|
|
24
|
+
]
|
storeframe/__main__.py
ADDED
storeframe/batch.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Frame a directory of captures laid out the fastlane way: ``<dir>/<locale>/*.png``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import shutil
|
|
6
|
+
from collections.abc import Callable, Iterable
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from PIL import Image
|
|
11
|
+
|
|
12
|
+
from . import platforms, render
|
|
13
|
+
from .design import Design
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class Report:
|
|
18
|
+
written: list[Path] = field(default_factory=list)
|
|
19
|
+
skipped: list[tuple[Path, str]] = field(default_factory=list)
|
|
20
|
+
missing_locales: list[str] = field(default_factory=list)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def frame_directory(
|
|
24
|
+
design: Design,
|
|
25
|
+
platform: str,
|
|
26
|
+
captured: str | Path,
|
|
27
|
+
out: str | Path,
|
|
28
|
+
*,
|
|
29
|
+
locales: Iterable[str] | None = None,
|
|
30
|
+
feature_graphic: bool = False,
|
|
31
|
+
clean: bool = True,
|
|
32
|
+
progress: Callable[[Path], None] | None = None,
|
|
33
|
+
) -> Report:
|
|
34
|
+
"""Frame every capture under ``captured/<locale>/`` into ``out/<locale>/``.
|
|
35
|
+
|
|
36
|
+
Each locale's output folder is emptied first (``clean``), so a renamed screen
|
|
37
|
+
does not leave a stale picture behind for the upload to find. Files that match
|
|
38
|
+
no screen of the design are skipped and reported.
|
|
39
|
+
"""
|
|
40
|
+
captured, out = Path(captured), Path(out)
|
|
41
|
+
platforms.get(platform)
|
|
42
|
+
report = Report()
|
|
43
|
+
|
|
44
|
+
if locales is None:
|
|
45
|
+
locales = sorted(p.name for p in captured.iterdir() if p.is_dir()) if captured.is_dir() else []
|
|
46
|
+
|
|
47
|
+
for locale in locales:
|
|
48
|
+
folder = captured / locale
|
|
49
|
+
if not folder.is_dir():
|
|
50
|
+
report.missing_locales.append(locale)
|
|
51
|
+
continue
|
|
52
|
+
target = out / locale
|
|
53
|
+
if clean:
|
|
54
|
+
shutil.rmtree(target, ignore_errors=True)
|
|
55
|
+
target.mkdir(parents=True, exist_ok=True)
|
|
56
|
+
|
|
57
|
+
feature_source: Path | None = None
|
|
58
|
+
for path in sorted(folder.glob("*.png")):
|
|
59
|
+
screen = design.match(path.stem)
|
|
60
|
+
if screen is None:
|
|
61
|
+
report.skipped.append((path, "matches no screen of the design"))
|
|
62
|
+
continue
|
|
63
|
+
with Image.open(path) as shot:
|
|
64
|
+
shot = shot.convert("RGB")
|
|
65
|
+
where = platforms.detect(platform, path.stem, shot.size)
|
|
66
|
+
picture = render.screenshot(shot, design, screen, kind=where.kind, device=where.device,
|
|
67
|
+
locale=locale, canvas_size=where.canvas)
|
|
68
|
+
destination = target / path.name
|
|
69
|
+
picture.save(destination, optimize=True)
|
|
70
|
+
report.written.append(destination)
|
|
71
|
+
if progress:
|
|
72
|
+
progress(destination)
|
|
73
|
+
if (feature_graphic and screen is design.screens[0] and screen.name in path.stem
|
|
74
|
+
and where.kind == next(iter(platforms.get(platform)))):
|
|
75
|
+
feature_source = path
|
|
76
|
+
|
|
77
|
+
if feature_graphic and feature_source is not None:
|
|
78
|
+
with Image.open(feature_source) as shot:
|
|
79
|
+
picture = render.feature_graphic(shot.convert("RGB"), design, design.screens[0],
|
|
80
|
+
locale=locale)
|
|
81
|
+
destination = target / f"{platforms.FEATURE_GRAPHIC_NAME}.png"
|
|
82
|
+
picture.save(destination, optimize=True)
|
|
83
|
+
report.written.append(destination)
|
|
84
|
+
if progress:
|
|
85
|
+
progress(destination)
|
|
86
|
+
|
|
87
|
+
return report
|
storeframe/cli.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""Command line: ``storeframe frame``, ``storeframe one``, ``storeframe devices``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from . import __version__, devices, platforms, render, styles
|
|
10
|
+
from .batch import frame_directory
|
|
11
|
+
from .design import Design
|
|
12
|
+
from .text import MissingFont
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _frame(args: argparse.Namespace) -> int:
|
|
16
|
+
design = Design.load(args.design)
|
|
17
|
+
report = frame_directory(
|
|
18
|
+
design, args.platform, args.captured, args.out,
|
|
19
|
+
locales=args.locale, feature_graphic=args.feature_graphic, clean=not args.keep,
|
|
20
|
+
progress=(lambda path: print(path)) if args.verbose else None,
|
|
21
|
+
)
|
|
22
|
+
for locale in report.missing_locales:
|
|
23
|
+
print(f"{locale}: no captures in {Path(args.captured) / locale}", file=sys.stderr)
|
|
24
|
+
for path, why in report.skipped:
|
|
25
|
+
print(f"skipped {path}: {why}", file=sys.stderr)
|
|
26
|
+
print(f"framed {len(report.written)} pictures into {args.out}")
|
|
27
|
+
return 0 if report.written else 1
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _one(args: argparse.Namespace) -> int:
|
|
31
|
+
from PIL import Image
|
|
32
|
+
|
|
33
|
+
design = Design.load(args.design)
|
|
34
|
+
with Image.open(args.capture) as shot:
|
|
35
|
+
shot = shot.convert("RGB")
|
|
36
|
+
if args.feature_graphic:
|
|
37
|
+
picture = render.feature_graphic(shot, design, args.screen, locale=args.locale,
|
|
38
|
+
device=args.device or "pixel-pro")
|
|
39
|
+
else:
|
|
40
|
+
where = platforms.detect(args.platform, Path(args.capture).stem, shot.size)
|
|
41
|
+
if args.kind:
|
|
42
|
+
where = platforms.get(args.platform).get(args.kind) or platforms.Target(
|
|
43
|
+
args.kind, args.device or where.device, where.canvas)
|
|
44
|
+
canvas = tuple(args.canvas) if args.canvas else where.canvas
|
|
45
|
+
picture = render.screenshot(shot, design, args.screen, kind=where.kind,
|
|
46
|
+
device=args.device or where.device, locale=args.locale,
|
|
47
|
+
canvas_size=canvas)
|
|
48
|
+
picture.save(args.output, optimize=True)
|
|
49
|
+
print(args.output)
|
|
50
|
+
return 0
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _devices(args: argparse.Namespace) -> int:
|
|
54
|
+
for device in devices.DEVICES.values():
|
|
55
|
+
print(f"{device.name:16} {device.title}")
|
|
56
|
+
print()
|
|
57
|
+
for name, targets in platforms.PLATFORMS.items():
|
|
58
|
+
for target in targets.values():
|
|
59
|
+
canvas = "x".join(map(str, target.canvas)) if target.canvas else "capture size"
|
|
60
|
+
print(f"{name:8} {target.kind:8} {target.device:16} {canvas}")
|
|
61
|
+
print()
|
|
62
|
+
print("styles:", ", ".join(styles.STYLES))
|
|
63
|
+
return 0
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def main(argv: list[str] | None = None) -> int:
|
|
67
|
+
parser = argparse.ArgumentParser(prog="storeframe", description="Frame app screenshots for the stores.")
|
|
68
|
+
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
69
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
70
|
+
|
|
71
|
+
frame = commands.add_parser("frame", help="frame a fastlane-style directory of captures")
|
|
72
|
+
frame.add_argument("design", type=Path, help="the design JSON")
|
|
73
|
+
frame.add_argument("--platform", required=True, choices=sorted(platforms.PLATFORMS))
|
|
74
|
+
frame.add_argument("--captured", required=True, type=Path, help="captures, one folder per locale")
|
|
75
|
+
frame.add_argument("--out", required=True, type=Path, help="framed output, one folder per locale")
|
|
76
|
+
frame.add_argument("--locale", action="append", help="only this locale (repeatable)")
|
|
77
|
+
frame.add_argument("--feature-graphic", action="store_true",
|
|
78
|
+
help="also draw Play's feature graphic from the first screen's phone capture")
|
|
79
|
+
frame.add_argument("--keep", action="store_true", help="don't empty the output locale folders first")
|
|
80
|
+
frame.add_argument("-v", "--verbose", action="store_true")
|
|
81
|
+
frame.set_defaults(run=_frame)
|
|
82
|
+
|
|
83
|
+
one = commands.add_parser("one", help="frame a single capture")
|
|
84
|
+
one.add_argument("design", type=Path)
|
|
85
|
+
one.add_argument("capture", type=Path)
|
|
86
|
+
one.add_argument("output", type=Path)
|
|
87
|
+
one.add_argument("--screen", required=True, help="screen name in the design")
|
|
88
|
+
one.add_argument("--platform", default="android", choices=sorted(platforms.PLATFORMS))
|
|
89
|
+
one.add_argument("--kind", help="layout kind (default: detected from file name / aspect)")
|
|
90
|
+
one.add_argument("--device", choices=sorted(devices.DEVICES), help="device body (default: platform's)")
|
|
91
|
+
one.add_argument("--canvas", type=int, nargs=2, metavar=("W", "H"), help="output size")
|
|
92
|
+
one.add_argument("--locale")
|
|
93
|
+
one.add_argument("--feature-graphic", action="store_true")
|
|
94
|
+
one.set_defaults(run=_one)
|
|
95
|
+
|
|
96
|
+
listing = commands.add_parser("devices", help="list devices, platforms and styles")
|
|
97
|
+
listing.set_defaults(run=_devices)
|
|
98
|
+
|
|
99
|
+
args = parser.parse_args(argv)
|
|
100
|
+
try:
|
|
101
|
+
return args.run(args)
|
|
102
|
+
except (MissingFont, ValueError, KeyError, FileNotFoundError) as error:
|
|
103
|
+
print(f"storeframe: {error}", file=sys.stderr)
|
|
104
|
+
return 2
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
if __name__ == "__main__":
|
|
108
|
+
sys.exit(main())
|
storeframe/design.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""The design file: one JSON per app describing its gallery.
|
|
2
|
+
|
|
3
|
+
{
|
|
4
|
+
"style": "plain" | "arc" | "tabs",
|
|
5
|
+
"font": "Nunito[wght].ttf", # optional, relative to this file; default bundled Nunito
|
|
6
|
+
"weights": ["Regular", "Bold"], # optional, per headline line
|
|
7
|
+
"fallback_locale": "en-US", # optional
|
|
8
|
+
"backgrounds": {"evening": ["#f2a65a", "#c9655b"]},
|
|
9
|
+
"chips": {"odt": "#4bb3e8"}, # tabs style: tab colours
|
|
10
|
+
"options": {}, # style options, e.g. {"sun": false}
|
|
11
|
+
"layouts": {"phone": {"screen_top": 0.22}}, # optional per-device-kind overrides
|
|
12
|
+
"params": {"phone": {"arc_peak": 0.15}}, # optional per-kind style-parameter overrides
|
|
13
|
+
"screens": [
|
|
14
|
+
{"name": "01-path", "background": "evening",
|
|
15
|
+
"headline": {"en-US": ["See when the sun", "is where."]},
|
|
16
|
+
"chips": ["odt"], "aliases": ["01-path-lite"]}
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
The ``frames.json`` files of OpenDocument.ios/.droid and almucantar load
|
|
21
|
+
unchanged: ``"arc": true`` means the arc style, a top-level ``chips`` palette
|
|
22
|
+
means the tabs style, ``_comment`` is ignored.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import json
|
|
28
|
+
from dataclasses import dataclass, field
|
|
29
|
+
from pathlib import Path
|
|
30
|
+
from typing import Any
|
|
31
|
+
|
|
32
|
+
from .paint import Colour
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class Screen:
|
|
37
|
+
name: str
|
|
38
|
+
background: tuple[Colour, Colour]
|
|
39
|
+
headline: dict[str, list[str]] = field(default_factory=dict)
|
|
40
|
+
chips: list[str] = field(default_factory=list)
|
|
41
|
+
aliases: list[str] = field(default_factory=list)
|
|
42
|
+
extra: dict[str, Any] = field(default_factory=dict)
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def names(self) -> list[str]:
|
|
46
|
+
return [self.name, *self.aliases]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass
|
|
50
|
+
class Design:
|
|
51
|
+
screens: list[Screen]
|
|
52
|
+
style: str = "plain"
|
|
53
|
+
font: str | None = None
|
|
54
|
+
weights: tuple[str, ...] = ("Regular", "Bold")
|
|
55
|
+
fallback_locale: str = "en-US"
|
|
56
|
+
chips: dict[str, str] = field(default_factory=dict)
|
|
57
|
+
options: dict[str, Any] = field(default_factory=dict)
|
|
58
|
+
layouts: dict[str, dict[str, Any]] = field(default_factory=dict)
|
|
59
|
+
params: dict[str, dict[str, Any]] = field(default_factory=dict)
|
|
60
|
+
path: Path | None = None
|
|
61
|
+
|
|
62
|
+
@classmethod
|
|
63
|
+
def load(cls, path: str | Path) -> Design:
|
|
64
|
+
path = Path(path)
|
|
65
|
+
return cls.from_dict(json.loads(path.read_text(encoding="utf-8")), root=path.parent, path=path)
|
|
66
|
+
|
|
67
|
+
@classmethod
|
|
68
|
+
def from_dict(cls, data: dict[str, Any], root: Path | None = None, path: Path | None = None) -> Design:
|
|
69
|
+
data = {k: v for k, v in data.items() if not k.startswith("_")}
|
|
70
|
+
backgrounds = data.get("backgrounds", {})
|
|
71
|
+
palette = data.get("chips", {})
|
|
72
|
+
|
|
73
|
+
style = data.get("style")
|
|
74
|
+
if style is None:
|
|
75
|
+
style = "arc" if data.get("arc") else "tabs" if palette else "plain"
|
|
76
|
+
|
|
77
|
+
font = data.get("font")
|
|
78
|
+
if font is not None:
|
|
79
|
+
candidate = Path(font)
|
|
80
|
+
if not candidate.is_absolute() and root is not None:
|
|
81
|
+
candidate = root / candidate
|
|
82
|
+
font = str(candidate)
|
|
83
|
+
|
|
84
|
+
screens = []
|
|
85
|
+
for entry in data.get("screens", []):
|
|
86
|
+
background = entry.get("background", ["#5b9be0", "#3a6fb8"])
|
|
87
|
+
if isinstance(background, str):
|
|
88
|
+
if background not in backgrounds:
|
|
89
|
+
raise ValueError(f"screen {entry['name']}: no background named {background!r}")
|
|
90
|
+
background = backgrounds[background]
|
|
91
|
+
chips = list(entry.get("chips", []))
|
|
92
|
+
missing = [name for name in chips if name not in palette]
|
|
93
|
+
if missing:
|
|
94
|
+
raise ValueError(f"screen {entry['name']}: no chip colour for {', '.join(missing)}")
|
|
95
|
+
known = {"name", "background", "headline", "chips", "aliases"}
|
|
96
|
+
screens.append(Screen(
|
|
97
|
+
name=entry["name"],
|
|
98
|
+
background=tuple(background),
|
|
99
|
+
headline={locale: list(lines) for locale, lines in entry.get("headline", {}).items()},
|
|
100
|
+
chips=chips,
|
|
101
|
+
aliases=list(entry.get("aliases", [])),
|
|
102
|
+
extra={k: v for k, v in entry.items() if k not in known},
|
|
103
|
+
))
|
|
104
|
+
|
|
105
|
+
return cls(
|
|
106
|
+
screens=screens,
|
|
107
|
+
style=style,
|
|
108
|
+
font=font,
|
|
109
|
+
weights=tuple(data.get("weights", ("Regular", "Bold"))),
|
|
110
|
+
fallback_locale=data.get("fallback_locale", "en-US"),
|
|
111
|
+
chips=dict(palette),
|
|
112
|
+
options=dict(data.get("options", {})),
|
|
113
|
+
layouts=dict(data.get("layouts", {})),
|
|
114
|
+
params=dict(data.get("params", {})),
|
|
115
|
+
path=path,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
def screen(self, name: str) -> Screen:
|
|
119
|
+
for screen in self.screens:
|
|
120
|
+
if name in screen.names:
|
|
121
|
+
return screen
|
|
122
|
+
raise KeyError(name)
|
|
123
|
+
|
|
124
|
+
def match(self, stem: str) -> Screen | None:
|
|
125
|
+
"""The screen a capture file belongs to: the longest screen name or alias
|
|
126
|
+
that the file stem ends with (so ``phone-01-path`` and
|
|
127
|
+
``iPhone 17 Pro Max-01-path`` both match ``01-path``)."""
|
|
128
|
+
best: tuple[int, Screen] | None = None
|
|
129
|
+
for screen in self.screens:
|
|
130
|
+
for name in screen.names:
|
|
131
|
+
matches = stem == name or stem.endswith(("-" + name, "_" + name))
|
|
132
|
+
if matches and (best is None or len(name) > best[0]):
|
|
133
|
+
best = (len(name), screen)
|
|
134
|
+
return best[1] if best else None
|
|
135
|
+
|
|
136
|
+
def order(self, screen: Screen) -> int:
|
|
137
|
+
return self.screens.index(screen)
|
|
138
|
+
|
|
139
|
+
def lines(self, screen: Screen, locale: str) -> list[str]:
|
|
140
|
+
"""The headline in that locale, falling back on its language, then the fallback locale."""
|
|
141
|
+
if locale in screen.headline:
|
|
142
|
+
return screen.headline[locale]
|
|
143
|
+
language = locale.replace("_", "-").split("-")[0]
|
|
144
|
+
for key, lines in screen.headline.items():
|
|
145
|
+
if key == language or key.split("-")[0] == language:
|
|
146
|
+
return lines
|
|
147
|
+
return screen.headline.get(self.fallback_locale, [])
|
storeframe/devices.py
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"""Drawn device bodies.
|
|
2
|
+
|
|
3
|
+
The device is drawn rather than pasted from a mockup image, so it takes the
|
|
4
|
+
shape of whatever was captured: a downloaded frame is the wrong shape for the
|
|
5
|
+
next device. Every proportion here is a fraction of the *screen* (or of the
|
|
6
|
+
body), never of the canvas, so a device looks the same at any size.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import bisect
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
|
|
14
|
+
from PIL import Image, ImageChops, ImageDraw, ImageFilter
|
|
15
|
+
|
|
16
|
+
from .geometry import Box, outset, squircle, stencil
|
|
17
|
+
from .layout import Layout
|
|
18
|
+
from .paint import horizontal_bands
|
|
19
|
+
|
|
20
|
+
# (how far down the body, how long), both fractions of the body's height
|
|
21
|
+
Key = tuple[float, float]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class Device:
|
|
26
|
+
name: str
|
|
27
|
+
title: str
|
|
28
|
+
aspect: float # nominal screen width / height, for telling captures apart
|
|
29
|
+
bezel: float # screen edge to outside of the body, of the screen width
|
|
30
|
+
rim: float # how much of the bezel is the black border (the rest is metal)
|
|
31
|
+
corner: float # screen corner radius, of the screen width
|
|
32
|
+
corner_easing: float = 2.2 # superellipse exponent: ~2.2 circular, ~5 continuous
|
|
33
|
+
buttons_left: tuple[Key, ...] = ()
|
|
34
|
+
buttons_right: tuple[Key, ...] = ()
|
|
35
|
+
tray: Key | None = None # a flush SIM tray seam on the left edge
|
|
36
|
+
hole: float | None = None # punch-hole camera diameter, of the screen width
|
|
37
|
+
hole_top: float = 0.0
|
|
38
|
+
island: tuple[float, float] | None = None # Dynamic Island (width, height), of the screen width
|
|
39
|
+
island_top: float = 0.0
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
DEVICES: dict[str, Device] = {
|
|
43
|
+
device.name: device
|
|
44
|
+
for device in (
|
|
45
|
+
# A Pixel 9/10 Pro XL: a 70.2mm screen in a 76.6mm body leaves 3.2mm of
|
|
46
|
+
# aluminium and black border each side; less than half of that is black.
|
|
47
|
+
# Near-circular display corners, a punch hole, both keys on the right and
|
|
48
|
+
# the SIM tray low on the left.
|
|
49
|
+
Device(
|
|
50
|
+
name="pixel-pro",
|
|
51
|
+
title="Google Pixel Pro (XL)",
|
|
52
|
+
aspect=1344 / 2992,
|
|
53
|
+
bezel=0.045,
|
|
54
|
+
rim=0.42,
|
|
55
|
+
corner=0.122,
|
|
56
|
+
corner_easing=2.2,
|
|
57
|
+
hole=0.046,
|
|
58
|
+
hole_top=0.028,
|
|
59
|
+
buttons_right=((0.175, 0.055), (0.250, 0.105)),
|
|
60
|
+
tray=(0.620, 0.075),
|
|
61
|
+
),
|
|
62
|
+
# A Pixel Tablet: a wide border that is mostly black mask, with a thin
|
|
63
|
+
# bright edge. Its keys are on the top edge in portrait, so none show.
|
|
64
|
+
Device(
|
|
65
|
+
name="pixel-tablet",
|
|
66
|
+
title="Google Pixel Tablet",
|
|
67
|
+
aspect=1600 / 2560,
|
|
68
|
+
bezel=0.068,
|
|
69
|
+
rim=0.86,
|
|
70
|
+
corner=0.030,
|
|
71
|
+
corner_easing=2.2,
|
|
72
|
+
),
|
|
73
|
+
# An iPhone Pro Max: continuous corners, the Dynamic Island, action and
|
|
74
|
+
# volume keys on the left, the side key on the right.
|
|
75
|
+
Device(
|
|
76
|
+
name="iphone-pro-max",
|
|
77
|
+
title="Apple iPhone Pro Max",
|
|
78
|
+
aspect=1320 / 2868,
|
|
79
|
+
bezel=0.042,
|
|
80
|
+
rim=0.57,
|
|
81
|
+
corner=0.141,
|
|
82
|
+
corner_easing=5.0,
|
|
83
|
+
island=(0.2826, 0.0811),
|
|
84
|
+
island_top=0.0334,
|
|
85
|
+
buttons_left=((0.189, 0.0423), (0.262, 0.0686), (0.349, 0.0686)),
|
|
86
|
+
buttons_right=((0.286, 0.1082),),
|
|
87
|
+
),
|
|
88
|
+
# An iPad Pro 13-inch: a uniform black border and a small corner.
|
|
89
|
+
Device(
|
|
90
|
+
name="ipad-pro-13",
|
|
91
|
+
title="Apple iPad Pro 13-inch",
|
|
92
|
+
aspect=2064 / 2752,
|
|
93
|
+
bezel=0.052,
|
|
94
|
+
rim=0.86,
|
|
95
|
+
corner=0.0174,
|
|
96
|
+
corner_easing=5.0,
|
|
97
|
+
),
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def get(name: str | Device) -> Device:
|
|
103
|
+
if isinstance(name, Device):
|
|
104
|
+
return name
|
|
105
|
+
try:
|
|
106
|
+
return DEVICES[name]
|
|
107
|
+
except KeyError:
|
|
108
|
+
raise ValueError(f"unknown device {name!r}; one of {', '.join(DEVICES)}") from None
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# Finish. The body is near-black; the metal is a warm grey (a multiplier per
|
|
112
|
+
# channel, small on purpose: past about 5% aluminium turns into gold).
|
|
113
|
+
BODY = "#08080a"
|
|
114
|
+
ALUMINIUM = (1.035, 1.0, 0.955)
|
|
115
|
+
BUTTON = ("#d8d8d6", "#a9a9a7", "#c4c4c2")
|
|
116
|
+
TRAY = ("#9a9a98", "#7c7c7a", "#909090")
|
|
117
|
+
GLASS = 96 # how brightly the screen edge catches the light, of 255
|
|
118
|
+
SHADOW = (0, 0, 0, 105)
|
|
119
|
+
SHADOW_OFFSET = (0.30, 0.65) # of the bezel, across and down
|
|
120
|
+
SHADOW_BLUR = 0.85 # of the bezel
|
|
121
|
+
KEY_STAND = 0.0061 # how far a key stands proud, of the screen width
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def chamfer(share: float) -> tuple[int, int, int]:
|
|
125
|
+
"""The polished metal's colour that far across the band, outside edge to black.
|
|
126
|
+
|
|
127
|
+
A narrow specular at the outer edge, a hard drop, a weaker sheen where the
|
|
128
|
+
flat turns down to the glass, and dark where it meets the black surround.
|
|
129
|
+
"""
|
|
130
|
+
stops = ((0.00, 150), (0.10, 240), (0.22, 208), (0.42, 138), (0.66, 192), (0.85, 164), (1.00, 96))
|
|
131
|
+
place = bisect.bisect_right([at for at, _ in stops], share)
|
|
132
|
+
if place == 0:
|
|
133
|
+
level = stops[0][1]
|
|
134
|
+
elif place == len(stops):
|
|
135
|
+
level = stops[-1][1]
|
|
136
|
+
else:
|
|
137
|
+
(before, low), (after, high) = stops[place - 1], stops[place]
|
|
138
|
+
level = low + (high - low) * (share - before) / (after - before)
|
|
139
|
+
|
|
140
|
+
return tuple(min(255, round(level * warm)) for warm in ALUMINIUM)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
@dataclass(frozen=True)
|
|
144
|
+
class Placed:
|
|
145
|
+
"""Where a device ended up on the canvas, in pixels."""
|
|
146
|
+
screen: Box
|
|
147
|
+
body: Box
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def place(canvas_size: tuple[int, int], capture_size: tuple[int, int], device: Device,
|
|
151
|
+
layout: Layout) -> Placed:
|
|
152
|
+
"""Fit the screen into the layout: as wide as allowed, leaving the foot free."""
|
|
153
|
+
width, height = canvas_size
|
|
154
|
+
aspect = capture_size[0] / capture_size[1]
|
|
155
|
+
top = layout.screen_top * height
|
|
156
|
+
# top + screen height + bottom bezel must stay above the foot
|
|
157
|
+
standing = height - layout.foot * height - top
|
|
158
|
+
screen_width = min(layout.screen_width * width, standing / (1 / aspect + device.bezel))
|
|
159
|
+
screen_height = screen_width / aspect
|
|
160
|
+
if layout.screen_left is None:
|
|
161
|
+
left = (width - screen_width) / 2
|
|
162
|
+
else:
|
|
163
|
+
left = layout.screen_left * width
|
|
164
|
+
bezel = device.bezel * screen_width
|
|
165
|
+
screen = (left, top, left + screen_width, top + screen_height)
|
|
166
|
+
body = (screen[0] - bezel, screen[1] - bezel, screen[2] + bezel, screen[3] + bezel)
|
|
167
|
+
|
|
168
|
+
return Placed(screen=screen, body=body)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def draw(canvas: Image.Image, capture: Image.Image, device: Device | str, layout: Layout) -> Placed:
|
|
172
|
+
"""Composite the capture behind glass in a drawn body, with keys and a shadow.
|
|
173
|
+
|
|
174
|
+
``canvas`` must be RGBA. Returns where the device was placed.
|
|
175
|
+
"""
|
|
176
|
+
device = get(device)
|
|
177
|
+
placed = place(canvas.size, capture.size, device, layout)
|
|
178
|
+
screen, body = placed.screen, placed.body
|
|
179
|
+
screen_width, screen_height = screen[2] - screen[0], screen[3] - screen[1]
|
|
180
|
+
bezel = device.bezel * screen_width
|
|
181
|
+
rim = bezel * device.rim
|
|
182
|
+
corner = device.corner * screen_width
|
|
183
|
+
|
|
184
|
+
# Built on its own transparent layer with room for the keys, composited once,
|
|
185
|
+
# so parts can be masked against each other without the ground showing through.
|
|
186
|
+
margin = round(bezel * 3)
|
|
187
|
+
origin = (round(body[0]) - margin, round(body[1]) - margin)
|
|
188
|
+
size = (round(body[2]) - origin[0] + margin, round(body[3]) - origin[1] + margin)
|
|
189
|
+
|
|
190
|
+
def here(box: Box) -> Box:
|
|
191
|
+
return tuple(v - origin[i % 2] for i, v in enumerate(box))
|
|
192
|
+
|
|
193
|
+
layer = Image.new("RGBA", size, (0, 0, 0, 0))
|
|
194
|
+
local_body = here(body)
|
|
195
|
+
tall_as = body[3] - body[1]
|
|
196
|
+
stand = screen_width * KEY_STAND
|
|
197
|
+
|
|
198
|
+
def along(edge: float, keys: tuple[Key, ...], proud: float) -> Image.Image:
|
|
199
|
+
marks = Image.new("L", size, 0)
|
|
200
|
+
pen = ImageDraw.Draw(marks)
|
|
201
|
+
for at_height, tall in keys:
|
|
202
|
+
y = local_body[1] + tall_as * at_height
|
|
203
|
+
pen.rounded_rectangle((edge - proud, y, edge + proud, y + tall_as * tall),
|
|
204
|
+
radius=proud * 0.55, fill=255)
|
|
205
|
+
return marks
|
|
206
|
+
|
|
207
|
+
# keys first, so the body's own edge covers where they meet it
|
|
208
|
+
keys = ImageChops.lighter(along(local_body[0], device.buttons_left, stand),
|
|
209
|
+
along(local_body[2], device.buttons_right, stand))
|
|
210
|
+
layer.paste(horizontal_bands(size, BUTTON), (0, 0), keys)
|
|
211
|
+
if device.tray:
|
|
212
|
+
layer.paste(horizontal_bands(size, TRAY), (0, 0),
|
|
213
|
+
along(local_body[0], (device.tray,), stand * 0.45))
|
|
214
|
+
|
|
215
|
+
# Every edge is the screen outline moved out, so border and metal keep their
|
|
216
|
+
# width all the way round.
|
|
217
|
+
face = squircle(here(screen), corner, device.corner_easing)
|
|
218
|
+
outline = outset(face, bezel)
|
|
219
|
+
|
|
220
|
+
# The metal band, filled as rings shaded by how far in they sit.
|
|
221
|
+
band = bezel - rim
|
|
222
|
+
lit = Image.new("RGB", size, chamfer(1.0))
|
|
223
|
+
rings = ImageDraw.Draw(lit)
|
|
224
|
+
steps = max(8, round(band))
|
|
225
|
+
for step in range(steps + 1):
|
|
226
|
+
share = step / steps
|
|
227
|
+
rings.polygon(outset(face, bezel - band * share), fill=chamfer(share))
|
|
228
|
+
layer.paste(lit, (0, 0), stencil(size, outline))
|
|
229
|
+
|
|
230
|
+
# the black surround, then the glass
|
|
231
|
+
layer.paste(Image.new("RGB", size, BODY), (0, 0), stencil(size, outset(face, rim)))
|
|
232
|
+
fitted = capture.convert("RGB").resize(
|
|
233
|
+
(round(screen_width), round(screen_height)), Image.LANCZOS).convert("RGBA")
|
|
234
|
+
inside = here(screen)
|
|
235
|
+
at = (round(inside[0]), round(inside[1]))
|
|
236
|
+
layer.paste(fitted, at, stencil(size, face).crop(
|
|
237
|
+
(at[0], at[1], at[0] + fitted.width, at[1] + fitted.height)))
|
|
238
|
+
|
|
239
|
+
# A hairline where the glass meets the surround, so a dark screen doesn't
|
|
240
|
+
# merge with the black border.
|
|
241
|
+
hair = max(1.0, screen_width * 0.0012)
|
|
242
|
+
halo = ImageChops.subtract(stencil(size, face), stencil(size, outset(face, -hair)))
|
|
243
|
+
layer.paste(Image.new("RGB", size, "white"), (0, 0),
|
|
244
|
+
halo.point(lambda level: level * GLASS // 255))
|
|
245
|
+
|
|
246
|
+
middle = (inside[0] + inside[2]) / 2
|
|
247
|
+
pen = ImageDraw.Draw(layer)
|
|
248
|
+
if device.hole:
|
|
249
|
+
across = device.hole * screen_width
|
|
250
|
+
top = inside[1] + device.hole_top * screen_width
|
|
251
|
+
pen.ellipse((middle - across / 2, top, middle + across / 2, top + across), fill=BODY)
|
|
252
|
+
if device.island:
|
|
253
|
+
island_width, island_height = (share * screen_width for share in device.island)
|
|
254
|
+
top = inside[1] + device.island_top * screen_width
|
|
255
|
+
pen.rounded_rectangle(
|
|
256
|
+
(middle - island_width / 2, top, middle + island_width / 2, top + island_height),
|
|
257
|
+
radius=island_height / 2, fill=BODY)
|
|
258
|
+
|
|
259
|
+
shadow = Image.new("RGBA", size, (0, 0, 0, 0))
|
|
260
|
+
shadow.paste(Image.new("RGB", size, SHADOW[:3]), (0, 0),
|
|
261
|
+
stencil(size, outline).point(lambda v: v * SHADOW[3] // 255))
|
|
262
|
+
canvas.alpha_composite(
|
|
263
|
+
shadow.filter(ImageFilter.GaussianBlur(bezel * SHADOW_BLUR)),
|
|
264
|
+
(origin[0] + round(bezel * SHADOW_OFFSET[0]), origin[1] + round(bezel * SHADOW_OFFSET[1])))
|
|
265
|
+
canvas.alpha_composite(layer, origin)
|
|
266
|
+
|
|
267
|
+
return placed
|
|
Binary file
|