data.py stays the reader (METRIC_RE now also captures the file date, same matches); common.py holds columns, steps, noise, sensor list, runs, file names. Duplicated run grouping, pose gaps, step bucketing, --table/--outside parsing removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
3.6 KiB
Python
69 lines
3.6 KiB
Python
"""GIF of the camera images over a range, to attach to a reply to the client.
|
|
|
|
Usage: .venv/bin/python tools/scripts/gif.py data/LENZSPITZE/
|
|
--start 2026-10-26T18:00Z --end 2026-10-27T08:00Z [--width 720] [--duration 700] [--max 150] [--zone X,Y,W,H] [--out FOLDER]
|
|
Takes the images of the folder whose time (file name, UTC) is between --start and --end inclusive, in order,
|
|
crops them if needed to --zone (pixels of the original image: top left corner, width, height), scales them to --width px,
|
|
writes the UTC date and time at the bottom right of each one and reduces them to 64 colours without dithering
|
|
(JPEG noise inflates a GIF: ~3 MB for a night of hourly images at 720 px). Beyond --max images, keeps
|
|
--max at regular intervals. Prints the time of each image kept, then the path of the GIF and its size.
|
|
"""
|
|
import argparse
|
|
import os
|
|
import sys
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
from data import find_files, image_time, iso, parse_time
|
|
|
|
|
|
def main():
|
|
a = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
a.add_argument("folder")
|
|
a.add_argument("--start", required=True, help="first time (UTC)")
|
|
a.add_argument("--end", required=True, help="last time (UTC), included")
|
|
a.add_argument("--width", type=int, default=720, help="width of the GIF in pixels (default 720)")
|
|
a.add_argument("--duration", type=int, default=700, help="duration of each image in ms (default 700)")
|
|
a.add_argument("--max", type=int, default=150, help="maximum number of images (default 150)")
|
|
a.add_argument("--zone", help="X,Y,W,H: crop in pixels of the original image (default: whole image)")
|
|
a.add_argument("--out", default=os.path.join("out", "diagnostic"), help="folder of the GIF")
|
|
o = a.parse_args()
|
|
|
|
start, end = parse_time(o.start), parse_time(o.end)
|
|
every = sorted((h, p) for p in find_files(o.folder)["images"] if (h := image_time(p)))
|
|
chosen = [(h, p) for h, p in every if start <= h <= end]
|
|
if not chosen:
|
|
available = f" (images from {iso(every[0][0])} to {iso(every[-1][0])})" if every else ""
|
|
sys.exit(f"No image between {iso(start)} and {iso(end)} in {o.folder}{available}.")
|
|
n = len(chosen)
|
|
if n > o.max:
|
|
chosen = [chosen[round(i * (n - 1) / (o.max - 1))] for i in range(o.max)]
|
|
print(f"{o.max} of {n} images kept")
|
|
|
|
frames = []
|
|
for h, p in chosen:
|
|
im = Image.open(p).convert("RGB")
|
|
if o.zone:
|
|
x, y, w, h_ = map(int, o.zone.split(","))
|
|
im = im.crop((x, y, x + w, y + h_))
|
|
im = im.resize((o.width, round(im.height * o.width / im.width)), Image.LANCZOS)
|
|
d = ImageDraw.Draw(im)
|
|
text = h.strftime("%Y-%m-%d %H:%M UTC")
|
|
font = ImageFont.load_default(size=max(12, o.width // 45))
|
|
left, top, right, bottom = d.textbbox((0, 0), text, font=font)
|
|
x, y, margin = im.width - (right - left) - 10, im.height - (bottom - top) - 10, 4
|
|
d.rectangle((x - margin, y - margin, x + right - left + margin, y + bottom - top + margin), fill="black")
|
|
d.text((x - left, y - top), text, fill="white", font=font)
|
|
frames.append(im.quantize(64, dither=Image.Dither.NONE))
|
|
print(iso(h))
|
|
|
|
os.makedirs(o.out, exist_ok=True)
|
|
name = f"images_{chosen[0][0]:%Y%m%dT%H%MZ}_{chosen[-1][0]:%Y%m%dT%H%MZ}.gif"
|
|
path = os.path.join(o.out, name)
|
|
frames[0].save(path, save_all=True, append_images=frames[1:], duration=o.duration, loop=0, optimize=True)
|
|
print(f"GIF: {path} ({os.path.getsize(path) / 1e6:.1f} MB, {len(frames)} images)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|