social-preview.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env python3
  2. """Draws the GitHub social preview (docs/img/social-preview.png, 1280 x 640) from its text below, the app icon and a
  3. picture of the workspace (Pillow).
  4. python3 scripts/social-preview.py [--window PICTURE] [--out docs/img/social-preview.png]
  5. --window: the workspace picture shown in the frame on the right, best the 2x render (3840 x 2160) of the hero
  6. (snap/s-desktop-all.png of scripts/site-shots.sh: Channel Rack, Piano Roll, Playlist and Mixer on the demo song).
  7. Default: website/assets/img/hero@2x.webp, else hero.webp. scripts/site-shots.py calls this with the render itself;
  8. tools/make_logo.py (site) calls it with the default after a logo change.
  9. The words live in TEXT below: change them here, never in the picture (README, website and this card say the same:
  10. the formats LYNX opens and exports, the plugin formats per OS, the repository URL). The fonts are Segoe UI (bold,
  11. semibold, regular) as the card always had; where Windows' fonts are not found (C:\\Windows\\Fonts, also through
  12. /mnt/c in WSL) the app's own Open Sans (resources/fonts) is used instead, and the script says so.
  13. """
  14. import argparse
  15. import os
  16. import sys
  17. from PIL import Image, ImageDraw, ImageFilter, ImageFont
  18. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  19. sys.path.insert(0, os.path.join(ROOT, "tools"))
  20. # --- the words ---------------------------------------------------------------------------------------------------------
  21. TEXT = {
  22. "title": "LYNX SHARE",
  23. "headline": ["FL Studio-style patterns,", "Ableton Live-style clips."],
  24. "lines": [
  25. "Channel Rack · Piano Roll · Playlist · Mixer",
  26. "Opens and exports .flp, .als, .flm, .dawproject",
  27. "VST3, CLAP, VST2, AU, LV2 · free and open source",
  28. ],
  29. "pills": ["Windows", "macOS", "Linux"],
  30. "site": "lynx-share.com",
  31. "url": "github.com/Ka1-S/LYNX-SHARE",
  32. }
  33. # --- the layout (1280 x 640) -------------------------------------------------------------------------------------------
  34. W, H = 1280, 640
  35. BG = (14, 15, 19) # the page
  36. GLOW = (51, 18, 23) # the red glow towards the right edge
  37. LEFT = 76 # the text column
  38. TILE_XY, TILE = (75, 105), 96 # the app icon
  39. TITLE_X, TITLE_MAX = 186, 81 # the name: left of it the icon, at most this size, centred on the icon
  40. HEAD_Y, HEAD_SIZE, HEAD_STEP = 222, 38, 46
  41. LINES_Y, LINES_SIZE, LINES_STEP = 340, 23, 36
  42. PILL_Y, PILL_H, PILL_SIZE, PILL_PAD, PILL_GAP = 486, 41, 20, 18, 11
  43. URL_Y, URL_SIZE = 566, 20
  44. WIN_X, WIN_Y, WIN_H = 625, 120, 507 # the window frame: 2 px border, the picture inside, cut by the right edge
  45. WIN_R = 10
  46. INK = {"title": (245, 245, 247), "head": (235, 235, 240), "lines": (170, 172, 182), "url": (120, 122, 132),
  47. "pill": (32, 34, 42), "pill_edge": (64, 66, 78), "pill_ink": (235, 235, 240), "site": (217, 48, 56),
  48. "site_ink": (255, 255, 255), "frame": (70, 72, 82)}
  49. def fonts():
  50. """(bold, semibold, regular) font files, and whether they are the card's own (Segoe UI)."""
  51. for d in ("/mnt/c/Windows/Fonts", "C:\\Windows\\Fonts", os.path.expanduser("~/.local/share/fonts"), "/usr/share/fonts/truetype/msttcorefonts"):
  52. files = [os.path.join(d, f) for f in ("segoeuib.ttf", "seguisb.ttf", "segoeui.ttf")]
  53. if all(os.path.isfile(f) for f in files):
  54. return files, True
  55. own = os.path.join(ROOT, "resources", "fonts")
  56. return [os.path.join(own, "OpenSans-Bold.ttf")] * 2 + [os.path.join(own, "OpenSans-Regular.ttf")], False
  57. def background():
  58. im = Image.new("RGB", (W, H), BG)
  59. px = im.load()
  60. for x in range(W):
  61. t = min(1.0, max(0.0, (x - 560) / 560.0))
  62. t = t * t * (3 - 2 * t)
  63. c = tuple(round(BG[i] + (GLOW[i] - BG[i]) * t) for i in range(3))
  64. for y in range(H):
  65. px[x, y] = c
  66. return im
  67. def window(src, h):
  68. """The workspace picture scaled to the frame's inner height."""
  69. im = Image.open(src).convert("RGB")
  70. w = round(im.width * h / im.height)
  71. return im.resize((w, h), Image.LANCZOS)
  72. def draw(win_src, out):
  73. files, own = fonts()
  74. if not own:
  75. print("social-preview: Segoe UI not found, using Open Sans (resources/fonts)")
  76. bold = lambda s: ImageFont.truetype(files[0], s)
  77. semi = lambda s: ImageFont.truetype(files[1], s)
  78. reg = lambda s: ImageFont.truetype(files[2], s)
  79. im = background().convert("RGBA")
  80. # the window: a soft shadow, the 2 px frame with round corners, the picture inside (cut by the right edge)
  81. pic = window(win_src, WIN_H - 4)
  82. fw = pic.width + 4
  83. shadow = Image.new("L", (W, H), 0)
  84. ImageDraw.Draw(shadow).rounded_rectangle((WIN_X - 6, WIN_Y + 4, WIN_X + fw + 6, WIN_Y + WIN_H + 14), WIN_R + 6, fill=170)
  85. shadow = shadow.filter(ImageFilter.GaussianBlur(18))
  86. dark = Image.new("RGBA", (W, H), (4, 3, 4, 0))
  87. dark.putalpha(shadow)
  88. im.alpha_composite(dark)
  89. k = 4
  90. mask = Image.new("L", (fw * k, WIN_H * k), 0)
  91. ImageDraw.Draw(mask).rounded_rectangle((0, 0, fw * k - 1, WIN_H * k - 1), WIN_R * k, fill=255)
  92. mask = mask.resize((fw, WIN_H), Image.LANCZOS)
  93. framed = Image.new("RGBA", (fw, WIN_H), INK["frame"] + (255,))
  94. inner = Image.new("L", (fw * k, WIN_H * k), 0)
  95. ImageDraw.Draw(inner).rounded_rectangle((2 * k, 2 * k, (fw - 2) * k - 1, (WIN_H - 2) * k - 1), (WIN_R - 2) * k, fill=255)
  96. inner = inner.resize((fw, WIN_H), Image.LANCZOS)
  97. framed.paste(pic, (2, 2), inner.crop((2, 2, 2 + pic.width, 2 + pic.height)))
  98. framed.putalpha(mask)
  99. im.alpha_composite(framed.crop((0, 0, min(fw, W - WIN_X), WIN_H)), (WIN_X, WIN_Y))
  100. d = ImageDraw.Draw(im)
  101. # the icon and the name, the name as large as fits before the window (at most TITLE_MAX), centred on the icon
  102. import make_logo
  103. im.alpha_composite(make_logo.Logo().icon(TILE), TILE_XY)
  104. size = TITLE_MAX
  105. while size > 40 and bold(size).getlength(TEXT["title"]) > WIN_X - 36 - TITLE_X:
  106. size -= 1
  107. f = bold(size)
  108. top, bottom = f.getbbox("LYNX")[1], f.getbbox("LYNX")[3]
  109. d.text((TITLE_X, TILE_XY[1] + TILE / 2 - (top + bottom) / 2), TEXT["title"], font=f, fill=INK["title"])
  110. for i, t in enumerate(TEXT["headline"]):
  111. d.text((LEFT - 2, HEAD_Y + i * HEAD_STEP), t, font=semi(HEAD_SIZE), fill=INK["head"])
  112. for i, t in enumerate(TEXT["lines"]):
  113. d.text((LEFT, LINES_Y + i * LINES_STEP), t, font=reg(LINES_SIZE), fill=INK["lines"])
  114. # the platforms, then the website in red
  115. f = semi(PILL_SIZE)
  116. x = LEFT
  117. for t, site in [(p, False) for p in TEXT["pills"]] + [(TEXT["site"], True)]:
  118. w = round(f.getlength(t)) + 2 * PILL_PAD
  119. box = (x, PILL_Y, x + w, PILL_Y + PILL_H)
  120. if site:
  121. d.rounded_rectangle(box, PILL_H // 2, fill=INK["site"])
  122. else:
  123. d.rounded_rectangle(box, PILL_H // 2, fill=INK["pill"], outline=INK["pill_edge"], width=1)
  124. d.text((x + w / 2, PILL_Y + PILL_H / 2), t, font=f, anchor="mm", fill=INK["site_ink" if site else "pill_ink"])
  125. x += w + PILL_GAP
  126. d.text((LEFT, URL_Y), TEXT["url"], font=reg(URL_SIZE), fill=INK["url"])
  127. # nothing may run into the window
  128. widest = max([LEFT + reg(LINES_SIZE).getlength(t) for t in TEXT["lines"]]
  129. + [LEFT + semi(HEAD_SIZE).getlength(t) for t in TEXT["headline"]] + [x - PILL_GAP])
  130. if widest > WIN_X - 12:
  131. sys.exit("social-preview: a line is %d px wide and runs into the window: shorten it" % (widest - LEFT))
  132. os.makedirs(os.path.dirname(os.path.abspath(out)), exist_ok=True)
  133. im.convert("RGB").save(out, optimize=True)
  134. print(" %s (%dx%d, window from %s)" % (os.path.relpath(out, ROOT), W, H, os.path.relpath(win_src, ROOT)))
  135. def main():
  136. ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
  137. ap.add_argument("--window")
  138. ap.add_argument("--out", default=os.path.join(ROOT, "docs", "img", "social-preview.png"))
  139. a = ap.parse_args()
  140. src = a.window
  141. if not src:
  142. img = os.path.join(ROOT, "website", "assets", "img")
  143. src = next((f for f in (os.path.join(img, "hero@2x.webp"), os.path.join(img, "hero.webp")) if os.path.isfile(f)), None)
  144. if not src or not os.path.isfile(src):
  145. sys.exit("social-preview: no workspace picture (give --window)")
  146. draw(src, a.out)
  147. return 0
  148. if __name__ == "__main__":
  149. sys.exit(main())