from PIL import Image from pathlib import Path import base64, io, html, os def load_frame(name): return Image.open(f"/mnt/data/{name}.png").convert("RGB") def crop_photo(frame_name, x, y, w=440, h=555): im = load_frame(frame_name) crop = im.crop((x, y, x+w, y+h)) crop.thumbnail((600, 760)) buf = io.BytesIO() crop.save(buf, format="JPEG", quality=88) return "data:image/jpeg;base64," + base64.b64encode(buf.getvalue()).decode() def pil_data(im): buf=io.BytesIO() im.thumbnail((700,900)) im.save(buf,format="JPEG",quality=90) return "data:image/jpeg;base64,"+base64.b64encode(buf.getvalue()).decode() prodi_img = Image.open("/mnt/data/shared_image.webp").convert("RGB") photo_sources = { 1: crop_photo("f2", 62, 360), 2: crop_photo("f2", 580, 360), 3: crop_photo("x2_5", 62, 600), 4: crop_photo("x2_5", 580, 600), 5: crop_photo("f3", 62, 750), 6: crop_photo("f3", 580, 750), 7: crop_photo("f4", 62, 420), 8: crop_photo("f4", 580, 420), 9: crop_photo("f5", 62, 365), 10: crop_photo("f5", 580, 365), 11: crop_photo("f6", 62, 285), 12: crop_photo("f6", 580, 285), 13: crop_photo("f7", 62, 900), 14: crop_photo("f7", 580, 900), 15: crop_photo("f9", 62, 190), 16: crop_photo("f9", 580, 190), 17: crop_photo("f10", 62, 700), 18: crop_photo("f10", 580, 700), 21: crop_photo("f12", 62, 900), 22: crop_photo("f12", 580, 900), 24: crop_photo("f13", 580, 900), 25: crop_photo("frame14", 62, 350), } photo_sources[19] = "https://upload.wikimedia.org/wikipedia/commons/b/bf/Gordonbrowndavos2008.jpg" photo_sources[20] = "https://upload.wikimedia.org/wikipedia/commons/a/a8/Angela_Merkel_2019_%28cropped%29.jpg" photo_sources[23] = "https://upload.wikimedia.org/wikipedia/commons/a/a6/Nicolas_Sarkozy_in_2010.jpg" photo_sources[26] = pil_data(prodi_img) cards = [ (1,"George W. Bush","United States","COMBATANT","US-led Operation Enduring Freedom, counterterrorism and combat operations alongside ISAF.","Requested portrait: 14 January 2003."), (2,"Stephen Harper","Canada","COMBATANT","Kandahar combat operations, counter-insurgency and reconstruction support.","Requested portrait: 2006 library photograph; brown-haired appearance."), (3,"Helen Clark","New Zealand","MID-COMBATANT","Bamyan Provincial Reconstruction Team, security patrols and Afghan development support.",""), (4,"John Howard","Australia","COMBATANT","Special operations, counterterrorism and Australian deployment in Uruzgan.","Requested portrait: Peter Morris photograph with flag background and smile."), (5,"Abdullah II","Jordan","MID-COMBATANT","Medical and humanitarian military assistance, including the Jordanian field hospital.",""), (6,"Branko Crvenkovski","North Macedonia","MID-COMBATANT","ISAF military participation, security and force-protection duties.",""), (7,"Sali Berisha","Albania","MID-COMBATANT","ISAF security contingent and military cooperation.",""), (8,"Stjepan Mesić","Croatia","MID-COMBATANT","ISAF military participation and security support.",""), (9,"Traian Băsescu","Romania","COMBATANT","Infantry deployments, security operations and operational support.",""), (10,"Ferenc Gyurcsány","Hungary","MID-COMBATANT","Provincial reconstruction, security and ISAF responsibilities.",""), (11,"Václav Klaus","Czech Republic","MID-COMBATANT","ISAF military assistance, medical and specialist support.",""), (12,"Lech Kaczyński","Poland","COMBATANT","Polish military deployment, security operations and combat-capable forces.",""), (13,"Valdas Adamkus","Lithuania","MID-COMBATANT","Chaghcharan Provincial Reconstruction Team and regional security.",""), (14,"Aigars Kalvītis","Latvia","MID-COMBATANT","ISAF personnel and military support responsibilities.",""), (15,"Toomas Hendrik Ilves","Estonia","COMBATANT","Forces operating in Helmand alongside British-led operations.",""), (16,"Fredrik Reinfeldt","Sweden","MID-COMBATANT","Northern Afghanistan security, reconstruction and Provincial Reconstruction Team activities.",""), (17,"Jens Stoltenberg","Norway","COMBATANT","Northern Afghanistan operations, military security and special-operations contributions.",""), (18,"Anders Fogh Rasmussen","Denmark","COMBATANT","Helmand combat operations and British-led coalition cooperation.",""), (19,"Gordon Brown","United Kingdom","COMBATANT","Helmand combat operations, counter-insurgency and coalition military support.","Requested photograph: World Economic Forum, Davos, 25 January 2008."), (20,"Angela Merkel","Germany","MID-COMBATANT","Northern Afghanistan, Kunduz Provincial Reconstruction Team, reconstruction and security.","Requested portrait reference: 2019 red-jacket photograph."), (21,"Jan Peter Balkenende","Netherlands","COMBATANT","Uruzgan deployment, operational security and counter-insurgency.","Requested portrait: 2006 portrait."), (22,"Guy Verhofstadt","Belgium","MID-COMBATANT","Kabul security and Belgian air-force support.",""), (23,"Nicolas Sarkozy","France","COMBATANT","French forces, operational security and counter-insurgency.","Requested photograph: EPP Summit, 28 October 2010; leaning back and looking sideways."), (24,"José Luis Rodríguez Zapatero","Spain","MID-COMBATANT","Herat and Badghis regional security, reconstruction and Provincial Reconstruction Team responsibilities.",""), (25,"José Sócrates","Portugal","MID-COMBATANT","ISAF military support, security and operational assistance.",""), (26,"Romano Prodi","Italy","MID-COMBATANT","Western Afghanistan, Herat regional responsibilities, security and reconstruction.","Exact user-supplied photograph: UN flag, orange-brown background, dark suit and bright red striped tie.") ] card_html=[] for num,name,country,status,desc,note in cards: cls="combat" if status=="COMBATANT" else "mid" source_note=f'

{html.escape(note)}

' if note else '' card_html.append(f"""
{html.escape(name)} portrait
{num:02d}. {html.escape(name)}
{html.escape(country)}
{status}

{html.escape(desc)}

{source_note}
""") page=f""" World Leaders & Afghanistan Deployments — 21 August 2007
Historical reference · Afghanistan War · 2001–2021

World Leaders & Afghanistan Deployments

All 26 leaders in the requested 21 August 2007 reference order. Two-column rounded black portrait boxes, with the requested custom status colours and concise contribution descriptions.

COMBATANTMID-COMBATANT
{''.join(card_html)}
""" out="/mnt/data/afghanistan_leaders_26_box_diagram.html" Path(out).write_text(page,encoding="utf-8") print("Created", out, "| cards:", len(cards), "| bytes:", os.path.getsize(out))