hidef banner rendering

This commit is contained in:
2026-02-15 17:12:12 +01:00
parent 8e4dd59f90
commit ded7290935
2 changed files with 18 additions and 8 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 84 KiB

View File

@@ -1,14 +1,24 @@
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
text = open("banner.txt", "r", encoding="utf-8").read() text = open("banner.txt", "r", encoding="utf-8").read()
font = ImageFont.truetype("DejaVuSansMono.ttf", 18)
dummy = Image.new("RGB", (1, 1)) scale = 4 # 26 is typical
pad = 10
font_px = 18
font = ImageFont.truetype("DejaVuSansMono.ttf", font_px * scale)
# Measure at high res
dummy = Image.new("RGB", (1, 1), "white")
d = ImageDraw.Draw(dummy) d = ImageDraw.Draw(dummy)
bbox = d.multiline_textbbox((0,0), text, font=font) bbox = d.multiline_textbbox((0, 0), text, font=font)
w, h = bbox[2]-bbox[0], bbox[3]-bbox[1] w, h = bbox[2] - bbox[0], bbox[3] - bbox[1]
img = Image.new("RGB", (w+20, h+20), "white") # Render at high res
d = ImageDraw.Draw(img) hi = Image.new("RGB", (w + 2*pad*scale, h + 2*pad*scale), "white")
d.multiline_text((10,10), text, font=font, fill="black") d = ImageDraw.Draw(hi)
img.save("banner.png") d.multiline_text((pad*scale, pad*scale), text, font=font, fill="black")
# Downscale with a good filter
out = hi.resize((hi.width // scale, hi.height // scale), resample=Image.Resampling.LANCZOS)
out.save("banner.png", dpi=(300, 300))