# label.py IN.png LABELS.json OUT.jpg "Title" [width] -> labelled JPG preview # (label names Start/Galerie/Gehege/Parkour/Ziel are the keys written by render_level.py) import sys, json from PIL import Image, ImageDraw, ImageFont src, lj, out, title = sys.argv[1:5] W = int(sys.argv[5]) if len(sys.argv) > 5 else 1280 im = Image.open(src).convert('RGB') im = im.resize((W, round(im.height * W / im.width)), Image.LANCZOS) d = ImageDraw.Draw(im, 'RGBA') F = '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf' f = ImageFont.truetype(F, 30); ft = ImageFont.truetype(F, 26) COL = {'Start': (255, 120, 40), 'Galerie': (255, 205, 40), 'Gehege': (120, 220, 90), 'Parkour': (80, 170, 255), 'Ziel': (200, 110, 255)} OFF = {'Galerie': (0, 0), 'Parkour': (0, -40), 'Ziel': (0, -30), 'Start': (0, -40), 'Gehege': (0, 0)} lab = json.load(open(lj)) for k, (u, v) in lab.items(): if not (0 <= u <= 1 and 0 <= v <= 1): continue x, y = u * im.width + OFF[k][0], v * im.height + OFF[k][1] tw, th = d.textbbox((0, 0), k, font=f)[2:] bx = (x - tw / 2 - 12, y - th / 2 - 8, x + tw / 2 + 12, y + th / 2 + 10) d.rounded_rectangle(bx, 10, fill=(20, 20, 30, 190), outline=COL[k] + (255,), width=3) d.text((x - tw / 2, y - th / 2), k, font=f, fill=COL[k]) if title: tw, th = d.textbbox((0, 0), title, font=ft)[2:] d.rectangle((0, 0, tw + 30, th + 20), fill=(20, 20, 30, 170)) d.text((15, 8), title, font=ft, fill=(255, 255, 255)) im.save(out, quality=85, optimize=True) print(out, im.size)