142 lines
5.9 KiB
Python
142 lines
5.9 KiB
Python
"""itch.io page images from one backdrop picture + the game title.
|
|
|
|
py -3.12 itch_images.py --title "Pipefix" --tagline "Lay the pipes. Beat the flow." \
|
|
--backdrop sprite-generator/output/backdrop_raw_00001_.png --out docs/itchio \
|
|
[--font Assets/Fonts/PressStart2P-Regular.ttf] [--text-font ...] [--color "#d9a441"] \
|
|
[--hero game/assets/Sprites/player.png] [--cover-art sprite-generator/output/itch_cover_raw_00001_.png]
|
|
|
|
Writes cover.png (630x500), banner_960x256.png, banner_1920x512.png, background_1920x1200.jpg.
|
|
--cover-art: own picture for the cover (otherwise the backdrop). Title on the cover at the top,
|
|
so the motif should leave the upper third calm. Use --cover-plain to keep the cover without text.
|
|
Without --font a Windows system font (Segoe UI Black / Arial Bold) is used.
|
|
Generalised from Pipefix/docs/itchio/make_images.py (that one also frames a board screenshot).
|
|
"""
|
|
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"]
|
|
SYSTEM_TEXT = [r"C:\Windows\Fonts\segoeui.ttf", r"C:\Windows\Fonts\arial.ttf"]
|
|
|
|
|
|
def first_existing(paths):
|
|
for p in paths:
|
|
if p and os.path.exists(p):
|
|
return p
|
|
raise SystemExit("no font found - pass --font")
|
|
|
|
|
|
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 backdrop(src, w, h, dark=0.55, blur=0):
|
|
img = cover_crop(src, w, h)
|
|
if blur:
|
|
img = img.filter(ImageFilter.GaussianBlur(blur))
|
|
img = ImageEnhance.Brightness(img).enhance(dark)
|
|
vig = Image.new("L", (w, h), 0)
|
|
ImageDraw.Draw(vig).ellipse((-w * 0.25, -h * 0.35, w * 1.25, h * 1.35), fill=255)
|
|
vig = vig.filter(ImageFilter.GaussianBlur(min(w, h) * 0.18))
|
|
return Image.composite(img, Image.new("RGB", (w, h), (0, 0, 0)), vig)
|
|
|
|
|
|
def fit_font(path, text, size, max_w):
|
|
while size > 8:
|
|
f = ImageFont.truetype(path, size)
|
|
if f.getbbox(text)[2] <= max_w:
|
|
return f
|
|
size -= 2
|
|
return ImageFont.truetype(path, size)
|
|
|
|
|
|
def title(draw, xy, text, font, color, anchor="mm"):
|
|
size = font.size
|
|
off = max(2, size // 12)
|
|
shadow = (12, 8, 4)
|
|
dark = tuple(c // 3 for c in color)
|
|
x, y = xy
|
|
draw.text((x + off, y + off), text, font=font, fill=shadow, anchor=anchor)
|
|
draw.text((x, y + off // 2), text, font=font, fill=dark, anchor=anchor)
|
|
draw.text((x, y), text, font=font, fill=color, anchor=anchor, stroke_width=max(1, size // 24), stroke_fill=shadow)
|
|
|
|
|
|
def text(draw, xy, s, font, fill=(230, 225, 215), anchor="mm"):
|
|
draw.text((xy[0] + 2, xy[1] + 2), s, font=font, fill=(10, 8, 6), anchor=anchor)
|
|
draw.text(xy, s, font=font, fill=fill, anchor=anchor)
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--title", required=True)
|
|
ap.add_argument("--tagline", default="")
|
|
ap.add_argument("--backdrop", required=True)
|
|
ap.add_argument("--out", default=".")
|
|
ap.add_argument("--font")
|
|
ap.add_argument("--text-font")
|
|
ap.add_argument("--color", default="#4fd8ff")
|
|
ap.add_argument("--hero", help="transparent PNG placed bottom-left on cover and banner")
|
|
ap.add_argument("--cover-art", help="own picture for the cover (default: backdrop)")
|
|
ap.add_argument("--cover-plain", action="store_true", help="cover without title/tagline")
|
|
a = ap.parse_args()
|
|
|
|
os.makedirs(a.out, exist_ok=True)
|
|
src = Image.open(a.backdrop).convert("RGB")
|
|
tfont = first_existing([a.font] + SYSTEM_FONTS)
|
|
xfont = first_existing([a.text_font, a.font] + SYSTEM_TEXT)
|
|
color = hex_rgb(a.color)
|
|
hero = Image.open(a.hero).convert("RGBA") if a.hero else None
|
|
|
|
def place_hero(img, size, xy):
|
|
if hero:
|
|
h = hero.copy()
|
|
h.thumbnail((size, size), Image.LANCZOS)
|
|
img.alpha_composite(h, xy)
|
|
|
|
# cover 630x500
|
|
w, h = 630, 500
|
|
csrc = Image.open(a.cover_art).convert("RGB") if a.cover_art else src
|
|
img = backdrop(csrc, w, h, 1.0 if a.cover_art else 0.7).convert("RGBA")
|
|
if not a.cover_art:
|
|
place_hero(img, 220, (30, h - 250))
|
|
if not a.cover_plain:
|
|
if a.cover_art: # darken the top behind the title, keep the motif bright
|
|
shade = Image.new("L", (w, h), 0)
|
|
ImageDraw.Draw(shade).rectangle((0, 0, w, int(h * 0.42)), fill=150)
|
|
shade = shade.filter(ImageFilter.GaussianBlur(40))
|
|
img = Image.composite(Image.new("RGBA", (w, h), (0, 0, 0, 255)), img, shade)
|
|
d = ImageDraw.Draw(img)
|
|
ty = 80 if a.cover_art else 150
|
|
title(d, (w // 2, ty), a.title, fit_font(tfont, a.title, 80, w - 60), color)
|
|
if a.tagline:
|
|
text(d, (w // 2, ty + 72), a.tagline, fit_font(xfont, a.tagline, 30, w - 60))
|
|
img.convert("RGB").save(os.path.join(a.out, "cover.png"), optimize=True)
|
|
|
|
# banners
|
|
for bw, bh in ((960, 256), (1920, 512)):
|
|
k = bh / 256
|
|
img = backdrop(src, bw, bh, 0.6).convert("RGBA")
|
|
place_hero(img, round(200 * k), (round(24 * k), round(40 * k)))
|
|
left = round((250 if hero else 40) * k)
|
|
d = ImageDraw.Draw(img)
|
|
title(d, (left, round(100 * k)), a.title, fit_font(tfont, a.title, round(70 * k), bw - left - 40), color, anchor="lm")
|
|
if a.tagline:
|
|
text(d, (left + 2, round(170 * k)), a.tagline, fit_font(xfont, a.tagline, round(32 * k), bw - left - 40), anchor="lm")
|
|
img.convert("RGB").save(os.path.join(a.out, f"banner_{bw}x{bh}.png"), optimize=True)
|
|
|
|
backdrop(src, 1920, 1200, 0.45, blur=3).save(os.path.join(a.out, "background_1920x1200.jpg"), quality=86)
|
|
print("ok ->", os.path.abspath(a.out))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|