85 lines
3.6 KiB
Python
85 lines
3.6 KiB
Python
|
|
"""Promo screenshots: real game screenshots framed on a (ComfyUI) backdrop with a short caption -
|
||
|
|
the store look. The game image itself is never altered.
|
||
|
|
|
||
|
|
py -3.12 itch_promo.py --shots docs/itchio/screenshots --captions docs/itchio/captions.txt \
|
||
|
|
--backdrop sprite-generator/output/itch_keyart_raw_00001_.png --out docs/itchio/promo \
|
||
|
|
[--size 1920x1080] [--font C:/Windows/Fonts/seguibl.ttf] [--color "#4fd8ff"]
|
||
|
|
|
||
|
|
captions.txt: one line per screenshot, "file.png | Caption text" (files without a line are skipped).
|
||
|
|
"""
|
||
|
|
import argparse
|
||
|
|
import os
|
||
|
|
|
||
|
|
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
|
||
|
|
|
||
|
|
SYSTEM_FONTS = [r"C:\Windows\Fonts\seguibl.ttf", r"C:\Windows\Fonts\arialbd.ttf"]
|
||
|
|
|
||
|
|
|
||
|
|
def hex_rgb(s):
|
||
|
|
s = s.lstrip("#")
|
||
|
|
return tuple(int(s[i:i + 2], 16) for i in (0, 2, 4))
|
||
|
|
|
||
|
|
|
||
|
|
def cover_crop(img, w, h):
|
||
|
|
k = max(w / img.width, h / img.height)
|
||
|
|
img = img.resize((round(img.width * k), round(img.height * k)), Image.LANCZOS)
|
||
|
|
x, y = (img.width - w) // 2, (img.height - h) // 2
|
||
|
|
return img.crop((x, y, x + w, y + h))
|
||
|
|
|
||
|
|
|
||
|
|
def fit_font(path, text, size, max_w):
|
||
|
|
while size > 10:
|
||
|
|
f = ImageFont.truetype(path, size)
|
||
|
|
if f.getbbox(text)[2] <= max_w:
|
||
|
|
return f
|
||
|
|
size -= 2
|
||
|
|
return ImageFont.truetype(path, size)
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
ap = argparse.ArgumentParser()
|
||
|
|
ap.add_argument("--shots", required=True)
|
||
|
|
ap.add_argument("--captions", required=True)
|
||
|
|
ap.add_argument("--backdrop", required=True)
|
||
|
|
ap.add_argument("--out", required=True)
|
||
|
|
ap.add_argument("--size", default="1920x1080")
|
||
|
|
ap.add_argument("--font")
|
||
|
|
ap.add_argument("--color", default="#4fd8ff")
|
||
|
|
a = ap.parse_args()
|
||
|
|
|
||
|
|
W, H = map(int, a.size.lower().split("x"))
|
||
|
|
font_path = a.font if a.font and os.path.exists(a.font) else next(p for p in SYSTEM_FONTS if os.path.exists(p))
|
||
|
|
accent = hex_rgb(a.color)
|
||
|
|
bg = cover_crop(Image.open(a.backdrop).convert("RGB"), W, H).filter(ImageFilter.GaussianBlur(6))
|
||
|
|
bg = ImageEnhance.Brightness(bg).enhance(0.45)
|
||
|
|
os.makedirs(a.out, exist_ok=True)
|
||
|
|
|
||
|
|
lines = [l.split("|", 1) for l in open(a.captions, encoding="utf-8-sig").read().splitlines() if "|" in l]
|
||
|
|
for i, (fname, caption) in enumerate(lines, 1):
|
||
|
|
fname, caption = fname.strip(), caption.strip()
|
||
|
|
shot = Image.open(os.path.join(a.shots, fname)).convert("RGB")
|
||
|
|
top = int(H * 0.19) # caption band
|
||
|
|
maxw, maxh = int(W * 0.86), H - top - int(H * 0.06)
|
||
|
|
k = min(maxw / shot.width, maxh / shot.height)
|
||
|
|
shot = shot.resize((round(shot.width * k), round(shot.height * k)), Image.LANCZOS)
|
||
|
|
img = bg.copy().convert("RGBA")
|
||
|
|
x, y = (W - shot.width) // 2, top
|
||
|
|
# shadow + thin accent frame
|
||
|
|
sh = Image.new("RGBA", (shot.width + 80, shot.height + 80), (0, 0, 0, 0))
|
||
|
|
ImageDraw.Draw(sh).rectangle((40, 40, 40 + shot.width, 40 + shot.height), fill=(0, 0, 0, 190))
|
||
|
|
img.alpha_composite(sh.filter(ImageFilter.GaussianBlur(22)), (x - 40 + 10, y - 40 + 16))
|
||
|
|
ImageDraw.Draw(img).rectangle((x - 4, y - 4, x + shot.width + 3, y + shot.height + 3), outline=accent, width=4)
|
||
|
|
img.paste(shot, (x, y))
|
||
|
|
d = ImageDraw.Draw(img)
|
||
|
|
f = fit_font(font_path, caption, int(H * 0.07), int(W * 0.9))
|
||
|
|
cy = top // 2
|
||
|
|
d.text((W // 2 + 3, cy + 3), caption, font=f, fill=(0, 0, 0), anchor="mm")
|
||
|
|
d.text((W // 2, cy), caption, font=f, fill=(255, 255, 255), anchor="mm", stroke_width=2, stroke_fill=(0, 0, 0))
|
||
|
|
out = os.path.join(a.out, f"promo_{i:02d}.png")
|
||
|
|
img.convert("RGB").save(out, optimize=True)
|
||
|
|
print(out)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|