"""Schermata del timer per gli store: accorcia le fasce nere vuote così i tasti
salgono e restano dentro la slide. Serve solo su Android: nella slide il
telefono Android è più lungo e la cornice ne taglia il fondo.

    python3 scripts/timer-store.py docs/store/schermate/android/05-timer.png
"""
import sys
from PIL import Image

for percorso in sys.argv[1:]:
    im = Image.open(percorso).convert('RGB')
    w, h = im.size
    g = im.convert('L')
    nero = [g.crop((0, y, w, y + 1)).getextrema()[1] < 16 for y in range(h)]
    lungo, corto = int(h * .1), int(h * .06)  # fasce oltre il 10% scendono al 6%
    tieni, y = [], 0
    while y < h:
        f = y
        while f < h and nero[f]:
            f += 1
        if f > y:
            tieni += range(y, y + (corto if f - y > lungo else f - y))
            y = f
        else:
            tieni.append(y)
            y += 1
    out = Image.new('RGB', (w, h), 'black')
    for i, riga in enumerate(tieni):
        out.paste(im.crop((0, riga, w, riga + 1)), (0, i))
    out.save(percorso)
    print(percorso, f'-{h - len(tieni)}px')
