From 21012adbec01a5ce723314cdadd2df5bdac9ae44 Mon Sep 17 00:00:00 2001 From: MangaValk Date: Sat, 24 Oct 2026 17:31:05 +0200 Subject: [PATCH] gif_to_360: --crop-x option for the horizontal crop position Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 2 +- tools/gif_to_360.py | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index e798b86..4c0adf6 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -128,7 +128,7 @@ AliExpress "ELM327 Mini V2.1 Bluetooth OBD **Dual Mode** 5.1" — dual mode = Bl - **LittleFS** works on the `spiffs` partition of `huge_app.csv` (~900 KB); the first mount after flashing formats it (prints `Corrupted dir pair` / `mount failed (-84)` and some `task_wdt ... task not found` once — harmless). - BLE (NimBLE) in the user's home: ~12 devices visible within 10 s. - Big fonts: generate with `npx lv_font_conv` (Node 22 installed) from `lvgl/scripts/generators/built_in_font/Montserrat-Medium.ttf`; `-DLV_LVGL_H_INCLUDE_SIMPLE` (in shared ini) makes the generated `#include` work. -- **GIF size after converting**: upscaling with LANCZOS can make a GIF ~3x bigger (7.2 MB source 498x298 -> 24.9 MB), because blended pixels plus per-frame palettes with dithering break the delta compression. Nearest-neighbour scaling gave 7.2 MB and looked as good. `tools/gif_to_360.py` retries with sharp scaling automatically when the result exceeds 11.5 MB. +- **GIF size after converting**: upscaling with LANCZOS can make a GIF ~3x bigger (7.2 MB source 498x298 -> 24.9 MB), because blended pixels plus per-frame palettes with dithering break the delta compression. Nearest-neighbour scaling gave 7.2 MB and looked as good. `tools/gif_to_360.py` retries with sharp scaling automatically when the result exceeds 11.5 MB. `--crop-x 0..1` moves the crop of wide GIFs (0 = left, 0.5 = center); `3d2c218d...gif` is converted with `--crop-x 0.36` (character is mostly left; best visible share inside the circle at 0.3-0.4). - Binary assets (GIFs): `board_build.embed_files = data/x.gif` → `extern const uint8_t x_start[] asm("_binary_data_x_gif_start")` (no huge C arrays). - Python edit scripts: never pass C strings containing `\n` through a bash heredoc into Python — it turned into a real line break twice; write the script with the Write tool instead. - Serial output from the background task (core 0) is often lost, not only right after reset; don't rely on it as proof. Serial output right after a reset can be lost/garbled (USB-CDC re-enumeration, prints from two cores interleave) — don't conclude a task isn't running from a missing early line. diff --git a/tools/gif_to_360.py b/tools/gif_to_360.py index 40a6ba3..cb8a173 100644 --- a/tools/gif_to_360.py +++ b/tools/gif_to_360.py @@ -6,7 +6,7 @@ If the result is too large, the GIF is redone with sharp (nearest-neighbour) sca Usage: pip install pillow - python gif_to_360.py [more ...] -o + python gif_to_360.py [more ...] -o [--crop-x 0..1] The device stores one background GIF in an 11.5 MB flash area: bigger results are reported as too large. """ @@ -20,12 +20,12 @@ SIZE = 360 MAX_BYTES = 11_500_000 -def convert(src, dst, resample=Image.LANCZOS): +def convert(src, dst, crop_x=0.5, resample=Image.LANCZOS): im = Image.open(src) w, h = im.size scale = max(SIZE / w, SIZE / h) nw, nh = max(SIZE, round(w * scale)), max(SIZE, round(h * scale)) - left, top = (nw - SIZE) // 2, (nh - SIZE) // 2 + left, top = round((nw - SIZE) * crop_x), (nh - SIZE) // 2 frames, durations = [], [] for frame in ImageSequence.Iterator(im): @@ -46,7 +46,7 @@ def convert(src, dst, resample=Image.LANCZOS): # smooth scaling blends pixels, so frames change everywhere and the per-frame palettes get # dithered: the GIF's delta compression stops working. Sharp scaling keeps it small. print(f"{os.path.basename(src)}: {size / 1e6:.2f} MB with smooth scaling, retrying with sharp scaling") - return convert(src, dst, Image.NEAREST) + return convert(src, dst, crop_x, Image.NEAREST) fps = len(durations) * 1000 / max(1, sum(durations)) note = "" if size <= MAX_BYTES else " ** TOO LARGE for the device (max 11.5 MB) **" print(f"{os.path.basename(src)}: {w}x{h}, {len(frames)} frames, {fps:.1f} fps " @@ -57,6 +57,8 @@ def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("inputs", nargs="+", help="GIF files or folders") ap.add_argument("-o", "--out", required=True, help="output folder") + ap.add_argument("--crop-x", type=float, default=0.5, + help="horizontal crop position for wide GIFs: 0 = left, 0.5 = center (default), 1 = right") args = ap.parse_args() os.makedirs(args.out, exist_ok=True) @@ -70,7 +72,7 @@ def main(): sys.exit("no GIF files found") for f in files: name = os.path.splitext(os.path.basename(f))[0] - convert(f, os.path.join(args.out, name + ".gif")) + convert(f, os.path.join(args.out, name + ".gif"), min(1.0, max(0.0, args.crop_x))) if __name__ == "__main__":