"""Crop the Amini teardrop mark out of the gold lockup.

The full lockup is three-and-a-half times as wide as it is tall. In a
header that already carries an eleven-item nav there is no width for it,
and shrinking it far enough to fit made the "Materials Trading" line
about four pixels tall - present, but unreadable, which is worse than
absent. The mark alone is nearly square, so it fits at a size where it
actually reads. The full lockup still runs in the footer and the mobile
drawer.

The cut is found rather than hard-coded: walk the columns from the left
and stop at the first run of blank ones wide enough to be the gap
between the mark and the wordmark.
"""

import os

from PIL import Image

OUT_DIR = "/home/abmtgrou/en.tupyfittings.com/media/brand"
GAP = 10

img = Image.open(os.path.join(OUT_DIR, "amini-logo-gold.png")).convert("RGBA")
w, h = img.size
px = img.load()

ink = []
for x in range(w):
    n = 0
    for y in range(h):
        if px[x, y][3] > 24:
            n += 1
    ink.append(n)

cut = w
run = 0
seen_ink = False
for x in range(w):
    if ink[x] == 0:
        if seen_ink:
            run += 1
            if run >= GAP:
                cut = x - run + 1
                break
    else:
        seen_ink = True
        run = 0

mark = img.crop((0, 0, cut, h))
mark = mark.crop(mark.getbbox())
mark.save(os.path.join(OUT_DIR, "amini-mark-gold.png"))
print("lockup %dx%d -> cut at %d -> mark %dx%d" % (w, h, cut, mark.size[0], mark.size[1]))