File size: 1,201 Bytes
5f70190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Fit a screenshot into the 1200x630 Open Graph image for the Space.

Cover-crops (scale-to-fill, centred) the given image to 1200x630 and writes
frontend/og.png — the file the OG tags in index.html and the README thumbnail
point at. Pass a screenshot of the app; re-run whenever you want a new one.

    python scripts/make_og.py ~/Desktop/shot.png
"""

import sys
from pathlib import Path

from PIL import Image

ROOT = Path(__file__).resolve().parent.parent
OUT = ROOT / "frontend" / "og.png"
W, H = 1200, 630  # the ratio Discord/OG previews expect (1.91:1)


def main():
    if len(sys.argv) < 2:
        print("usage: python scripts/make_og.py <screenshot>")
        return 1
    src = Image.open(sys.argv[1]).convert("RGB")
    sw, sh = src.size
    scale = max(W / sw, H / sh)  # cover: fill the frame, crop the overflow
    resized = src.resize((round(sw * scale), round(sh * scale)), Image.LANCZOS)
    nw, nh = resized.size
    left, top = (nw - W) // 2, (nh - H) // 2
    resized.crop((left, top, left + W, top + H)).save(OUT, "PNG", optimize=True)
    print(f"wrote {OUT.relative_to(ROOT)} ({OUT.stat().st_size // 1024} KB, {W}x{H})")


if __name__ == "__main__":
    sys.exit(main())