"""11 — Les documents d'origine montrés dans les murs de la maquette (demande de Jules, 08/10 : « se référer aux documents de base », chaque fiche ouvre sa planche en entier). - Les planches du concours Habiter Demain 2020, entières, avec leur texte, depuis le PDF d'impression (le rendu « léger » du Corpus est à 144 dpi ; celui-ci a ses images à 400-450 dpi). La planche 9 n'est pas reprise : elle porte le « −21 % » national retiré des fiches (relecture du 08/10). - Du recueil de calculs, les dessins seulement (découpes du 05/10), jamais les pages entières : presque chaque page porte une incohérence signalée par la transcription (cuve de 8,4 m³, 6 ha ou 6 000 m², badges 97 %…). Chaque document sort en trois tailles : -g (3 300 px de large au plus, pour lire le texte en zoomant), -m (1 600 px, chargée à l'ouverture du mur), -p (360 px, la vignette du bouton de la fiche). documents.json donne la taille de la plus grande version et si -g existe. Usage : python 11_documents.py """ import sys, os, json import pymupdf from PIL import Image pdf, rec, out, manifeste = sys.argv[1:5] os.makedirs(out, exist_ok=True) PAPIER = (255, 253, 246) docs = {} def ecrit(nom, im): im = im.convert("RGB"); grand = im.width > 1700 # une petite figure n'a pas de version -g for suf, larg, q in (("g", 3300, 80), ("m", 1600, 80), ("p", 360, 78)): if suf == "g" and not grand: continue t = im.copy() if t.width > larg: t = t.resize((larg, round(t.height * larg / t.width)), Image.LANCZOS) t.save(os.path.join(out, f"{nom}-{suf}.jpg"), quality=q, optimize=True, progressive=True) if suf == ("g" if grand else "m"): docs[nom] = [t.width, t.height, int(grand)] def ouvre(nom): im = Image.open(os.path.join(rec, nom)).convert("RGBA") if nom.startswith("plantes/"): # les vignettes de plantes ont perdu leur masque à l'extraction : leur fond est noir import numpy as np a = np.asarray(im).copy(); a[a[..., :3].max(2) < 60, 3] = 0; im = Image.fromarray(a, "RGBA") fond = Image.new("RGBA", im.size, PAPIER + (255,)); fond.alpha_composite(im); return fond def rangee(images, marge=0.06, bas=True, fond=PAPIER): h = max(i.height for i in images); g = int(h * marge) w = sum(i.width for i in images) + g * (len(images) + 1) r = Image.new("RGBA", (w, h + 2 * g), fond + (255,)); x = g for i in images: r.alpha_composite(i, (x, g + (h - i.height if bas else (h - i.height) // 2))); x += i.width + g return r def pile(images, marge=0.04): w = max(i.width for i in images); g = int(w * marge) r = Image.new("RGBA", (w + 2 * g, sum(i.height for i in images) + g * (len(images) + 1)), PAPIER + (255,)); y = g for i in images: r.alpha_composite(i, (g + (w - i.width) // 2, y)); y += i.height + g return r # ---- les planches du concours, entières d = pymupdf.open(pdf) for n in (1, 2, 3, 4, 5, 6, 7, 8): pix = d[n - 1].get_pixmap(dpi=200) ecrit(f"hd{n}", Image.frombytes("RGB", (pix.width, pix.height), pix.samples)) # ---- les dessins du recueil F = lambda x: "figures/" + x; P = lambda x: "pictos/" + x ecrit("r-recuperation", ouvre(F("p16-axo-maison-recuperation.png"))) ecrit("r-grises", ouvre(F("p18-axo-maison-eaux-grises.png"))) ecrit("r-noue", rangee([ouvre(P("p18-noue-absorption.png")), ouvre(P("p19-noue-type.png"))], bas=False)) plantes = ["petasites-hybridus", "lysimachia-vulgaris", "lythrum-salicaria", "juncus", "myriophyllum-aquaticum", "cyperus-papyrus", "cyperus-papyrus-bis"] ecrit("r-plantes", rangee([ouvre(f"plantes/p19-{p}.png") for p in plantes], marge=0.12)) ecrit("r-phyto-carte", ouvre(F("p20-carte-implantation-phytoepuration.png"))) ecrit("r-phyto-coupes", pile([ouvre(F("p20-coupe-etapes-1-2-biomethanisation.png")), ouvre(F("p21-coupe-bassins-3-6.png"))])) arbres = sorted(f for f in os.listdir(os.path.join(rec, "arbres")) if f.endswith(".png")) ecrit("r-haie", rangee([ouvre("arbres/" + a) for a in arbres], marge=0.03)) ecrit("r-parcelle", rangee([ouvre(P("p12-parcelle-bordee-de-haies.png")), ouvre(P("p12-arbre-fruitier-racines.png"))], bas=False)) ecrit("r-strates", ouvre(F("p12-strates-production.png"))) ecrit("r-noue-arbre", ouvre(P("p19-coupe-noue-arbre.png"))) ecrit("r-compost", ouvre(F("p28-compost-serre-chaleur.png"))) json.dump(docs, open(manifeste, "w"), indent=0) print(len(docs), "documents")