Files
Support-Agent/agent-tools/gif.py
T
maximeandClaude Opus 5.5 56b93e9b82 Agent tools: reading tools moved to agent-tools/ (D-081)
The reading tools leave agents/diagnostic/outils/ for a root folder of their
own; paths updated in skills, agents, detectors, scripts, case fiches,
support/, contexts and CLAUDE.md; graphique, vignettes and gif find the repo
root one level up; history keeps old paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 16:36:45 +01:00

66 lines
3.4 KiB
Python

"""GIF des images de la caméra sur une plage, pour joindre à une réponse au client (skill /support).
Usage : .venv/bin/python agent-tools/gif.py data/LENZSPITZE/
--debut 2026-10-26T18:00Z --fin 2026-10-27T08:00Z [--largeur 720] [--duree 700] [--max 150] [--sortie DOSSIER]
Prend les images du dossier dont l'heure (nom du fichier, UTC) est entre --debut et --fin inclus, dans l'ordre, les
réduit à --largeur px, écrit la date et l'heure UTC en bas à droite de chacune et les ramène à 64 teintes sans tramage
(le bruit des JPEG gonfle un GIF : ~3 Mo pour une nuit d'images horaires à 720 px). Au-delà de --max images, en garde
--max à intervalle régulier. Imprime l'heure de chaque image retenue, puis le chemin du GIF et sa taille.
"""
import argparse
import os
import sys
from PIL import Image, ImageDraw, ImageFont
from donnees import heure_image, instant, iso, trouver
RACINE = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
def main():
a = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
a.add_argument("dossier")
a.add_argument("--debut", required=True, help="première heure (UTC)")
a.add_argument("--fin", required=True, help="dernière heure (UTC), incluse")
a.add_argument("--largeur", type=int, default=720, help="largeur du GIF en pixels (défaut 720)")
a.add_argument("--duree", type=int, default=700, help="durée de chaque image en ms (défaut 700)")
a.add_argument("--max", type=int, default=150, help="nombre maximal d'images (défaut 150)")
a.add_argument("--sortie", default=os.path.join(RACINE, "out", "diagnostic"), help="dossier du GIF")
o = a.parse_args()
debut, fin = instant(o.debut), instant(o.fin)
toutes = sorted((h, p) for p in trouver(o.dossier)["images"] if (h := heure_image(p)))
choisies = [(h, p) for h, p in toutes if debut <= h <= fin]
if not choisies:
dispo = f" (images du {iso(toutes[0][0])} au {iso(toutes[-1][0])})" if toutes else ""
sys.exit(f"Aucune image entre {iso(debut)} et {iso(fin)} dans {o.dossier}{dispo}.")
n = len(choisies)
if n > o.max:
choisies = [choisies[round(i * (n - 1) / (o.max - 1))] for i in range(o.max)]
print(f"{o.max} images sur {n} gardées")
vues = []
for h, p in choisies:
im = Image.open(p).convert("RGB")
im = im.resize((o.largeur, round(im.height * o.largeur / im.width)), Image.LANCZOS)
d = ImageDraw.Draw(im)
texte = h.strftime("%Y-%m-%d %H:%M UTC")
police = ImageFont.load_default(size=max(12, o.largeur // 45))
g, haut, dr, bas = d.textbbox((0, 0), texte, font=police)
x, y, marge = im.width - (dr - g) - 10, im.height - (bas - haut) - 10, 4
d.rectangle((x - marge, y - marge, x + dr - g + marge, y + bas - haut + marge), fill="black")
d.text((x - g, y - haut), texte, fill="white", font=police)
vues.append(im.quantize(64, dither=Image.Dither.NONE))
print(iso(h))
os.makedirs(o.sortie, exist_ok=True)
nom = f"images_{choisies[0][0]:%Y%m%dT%H%MZ}_{choisies[-1][0]:%Y%m%dT%H%MZ}.gif"
chemin = os.path.join(o.sortie, nom)
vues[0].save(chemin, save_all=True, append_images=vues[1:], duration=o.duree, loop=0, optimize=True)
print(f"GIF : {chemin} ({os.path.getsize(chemin) / 1e6:.1f} Mo, {len(vues)} images)")
if __name__ == "__main__":
main()