gif_to_360: retry with sharp scaling when too large; add new GIF
Smooth upscaling blends pixels and dithers per-frame palettes, which breaks GIF delta compression (7.2 MB source -> 24.9 MB). Nearest- neighbour scaling keeps it at 7.2 MB, under the 11.5 MB flash limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
15b4ef5cef
commit
b2f6a52902
3 files changed
+9
-2
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 6.9 MiB |
@@ -128,6 +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).
|
- **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.
|
- 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.
|
- 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.
|
||||||
- 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).
|
- 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.
|
- 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.
|
- 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.
|
||||||
|
|||||||
+8
-2
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
Each frame is scaled so the GIF covers 360x360, then center-cropped. Frame durations and looping are kept.
|
Each frame is scaled so the GIF covers 360x360, then center-cropped. Frame durations and looping are kept.
|
||||||
Pillow stores only the changed part of each frame, which keeps files small.
|
Pillow stores only the changed part of each frame, which keeps files small.
|
||||||
|
If the result is too large, the GIF is redone with sharp (nearest-neighbour) scaling, which is often much smaller.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
pip install pillow
|
pip install pillow
|
||||||
@@ -19,7 +20,7 @@ SIZE = 360
|
|||||||
MAX_BYTES = 11_500_000
|
MAX_BYTES = 11_500_000
|
||||||
|
|
||||||
|
|
||||||
def convert(src, dst):
|
def convert(src, dst, resample=Image.LANCZOS):
|
||||||
im = Image.open(src)
|
im = Image.open(src)
|
||||||
w, h = im.size
|
w, h = im.size
|
||||||
scale = max(SIZE / w, SIZE / h)
|
scale = max(SIZE / w, SIZE / h)
|
||||||
@@ -31,7 +32,7 @@ def convert(src, dst):
|
|||||||
durations.append(frame.info.get("duration", im.info.get("duration", 100)) or 100)
|
durations.append(frame.info.get("duration", im.info.get("duration", 100)) or 100)
|
||||||
rgba = frame.convert("RGBA") # fully composited frame
|
rgba = frame.convert("RGBA") # fully composited frame
|
||||||
if (nw, nh) != (w, h):
|
if (nw, nh) != (w, h):
|
||||||
rgba = rgba.resize((nw, nh), Image.LANCZOS)
|
rgba = rgba.resize((nw, nh), resample)
|
||||||
rgba = rgba.crop((left, top, left + SIZE, top + SIZE))
|
rgba = rgba.crop((left, top, left + SIZE, top + SIZE))
|
||||||
# the device shows the GIF on black: flatten transparency onto black
|
# the device shows the GIF on black: flatten transparency onto black
|
||||||
bg = Image.new("RGBA", rgba.size, (0, 0, 0, 255))
|
bg = Image.new("RGBA", rgba.size, (0, 0, 0, 255))
|
||||||
@@ -41,6 +42,11 @@ def convert(src, dst):
|
|||||||
frames[0].save(dst, save_all=True, append_images=frames[1:], duration=durations,
|
frames[0].save(dst, save_all=True, append_images=frames[1:], duration=durations,
|
||||||
loop=im.info.get("loop", 0), optimize=True, disposal=1)
|
loop=im.info.get("loop", 0), optimize=True, disposal=1)
|
||||||
size = os.path.getsize(dst)
|
size = os.path.getsize(dst)
|
||||||
|
if size > MAX_BYTES and resample != Image.NEAREST and (nw, nh) != (w, h):
|
||||||
|
# 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)
|
||||||
fps = len(durations) * 1000 / max(1, sum(durations))
|
fps = len(durations) * 1000 / max(1, sum(durations))
|
||||||
note = "" if size <= MAX_BYTES else " ** TOO LARGE for the device (max 11.5 MB) **"
|
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 "
|
print(f"{os.path.basename(src)}: {w}x{h}, {len(frames)} frames, {fps:.1f} fps "
|
||||||
|
|||||||
Reference in new issue
Block a user