#!/usr/bin/env python3
from pathlib import Path
from PIL import Image,ImageDraw,ImageFont
import json,sys,math
root=Path('/tmp/lead-28684-r1/evidence-capture')
side=sys.argv[1]
manifest=json.loads((root/'capture-manifest.json').read_text())
files=[]
for run in manifest[side].values():files.extend(sorted(Path(run['run']).glob('*.png')))
font=ImageFont.truetype('/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',14)
for offset in range(0,len(files),6):
 subset=files[offset:offset+6];gallery=Image.new('RGB',(1440,720),(40,40,40));draw=ImageDraw.Draw(gallery)
 for i,file in enumerate(subset):
  im=Image.open(file).convert('RGB');im.thumbnail((480,330));x=(i%3)*480;y=(i//3)*360;gallery.paste(im,(x,y+30));draw.text((x+4,y+4),file.name,font=font,fill='white')
 out=root/f'{side}-review-{offset//6+1}.png';gallery.save(out);print(out)
