gif_to_360: --crop-x option for the horizontal crop position

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-10-24 17:31:05 +02:00
1 parent b2f6a52902
commit 21012adbec
2 files changed
+8 -6

No files matched your search

+7 -5
View File
@@ -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 <input.gif or folder> [more ...] -o <output folder>
python gif_to_360.py <input.gif or folder> [more ...] -o <output folder> [--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__":