"""Replace the scythe standing in for the first T of CUTTERS with a copy of the second T. py -I Tools/ui/logo_t.py in.png out.png [preview.png] The second T (white letter) is copied left by the letter pitch of the word, given a dark outline as thick as the rest of the logo's, and the scythe pixels (orange handle, grey head, pale blade) are cleared or blacked in. """ import sys import numpy as np from PIL import Image from scipy import ndimage as nd src, dst = sys.argv[1], sys.argv[2] im = np.array(Image.open(src).convert("RGBA")).astype(np.int32) H, W = im.shape[:2] r, g, b, a = im[..., 0], im[..., 1], im[..., 2], im[..., 3] mx, mn = np.maximum(np.maximum(r, g), b), np.minimum(np.minimum(r, g), b) solid = a > 128 white = solid & (mn > 200) & (mx - mn < 30) dark = solid & (mx < 90) orange = solid & (r > 150) & (r - b > 80) & (g < 200) grey = solid & (mx - mn < 35) & (mn > 100) & (mn <= 215) # letters: big white components. The middle row (CUTTERS) is the band holding the orange handle. lab, n = nd.label(white) objs = nd.find_objects(lab) sizes = nd.sum(white, lab, range(1, n + 1)) ys, xs = np.nonzero(orange) hx0, hx1, hy0, hy1 = xs.min(), xs.max(), ys.min(), ys.max() row_mid = (hy0 + hy1) / 2 letters = [] for i, sl in enumerate(objs): if sizes[i] < 0.002 * H * W: continue y0, y1, x0, x1 = sl[0].start, sl[0].stop, sl[1].start, sl[1].stop if y0 < row_mid < y1: letters.append((x0, i + 1, x1, y0, y1)) letters.sort() print("row letters (x0..x1):", [(l[0], l[2]) for l in letters]) # the first letter right of the handle is the second T, the one after it is E; the one left of it is U right = [l for l in letters if l[0] > hx0] left = [l for l in letters if l[2] < hx0 + 5] t2, e = right[0], right[1] u = left[-1] # T1 goes halfway between U and T2 so both gaps match gap_te = e[0] - t2[2] w_t = t2[2] - t2[0] x_new = int(round((u[2] + t2[0] - w_t) / 2)) dx = x_new - t2[0] print(f"U ends {u[2]}, T2 {t2[0]}..{t2[2]}, E starts {e[0]}; new T at {x_new} (dx {dx})") t2mask = lab == t2[1] newT = np.zeros_like(t2mask) if dx < 0: newT[:, :W + dx] = t2mask[:, -dx:] # outline thickness: distance from the T2 white to the transparent outside, measured across its stem's left side dist_out = nd.distance_transform_edt(~solid) outline = int(np.percentile(nd.distance_transform_edt(~t2mask)[(solid) & (~white) & (nd.binary_dilation(t2mask, iterations=40)) & (~nd.binary_dilation(t2mask, iterations=3))], 95)) outline = max(8, min(outline, 60)) print("outline px", outline) # the scythe: everything light or coloured between U and T2, from the top of the handle to its foot, that is not one of the letters box = np.zeros_like(solid) box[max(0, hy0 - 15):hy1 + 15, u[2] - 10:t2[0] + 30] = True scythe = box & solid & ~dark & ~np.isin(lab, [u[1], t2[1]]) scythe = nd.binary_dilation(scythe, iterations=3) & solid & box & ~np.isin(lab, [u[1], t2[1]]) big = np.isin(lab, [l[1] for l in letters]) out = im.copy() # what the outline of everything else covers stays dark, the rest of the scythe goes transparent keep_dark = nd.distance_transform_edt(~(white & ~scythe)) <= outline + 1 ring = nd.distance_transform_edt(~newT) <= outline dark_col = np.median(im[dark & ~scythe][:, :3], axis=0).astype(np.int32) clear = scythe & ~keep_dark & ~ring out[clear] = 0 # gaps the handle left inside the word (between it and T2) are closed up: transparent pockets in the box that the filled silhouette surrounds pocket = box & (a < 250) & nd.binary_fill_holes(solid | ring) # inside the box every solid pixel off the letters (and their soft edges) becomes plain outline: no ghost of the handle's edge letters_soft = nd.binary_dilation(np.isin(lab, [u[1], t2[1]]), iterations=1) | nd.binary_dilation(newT, iterations=2) fill = (scythe & (keep_dark | ring)) | (ring & ~solid) | pocket | (box & (solid | ring) & ~letters_soft & (keep_dark | ring)) out[fill, :3] = dark_col out[fill, 3] = 255 # the letter itself: the T2 pixels copied across with their shading for c in range(4): plane = out[..., c] shifted = np.zeros_like(plane) shifted[:, :W + dx] = im[:, -dx:, c] soft = nd.binary_dilation(newT, iterations=2) plane[soft] = shifted[soft] # soften the outer edge of the new outline a touch edge = ring & ~nd.binary_erosion(ring, iterations=1) & ~solid out[edge, 3] = 200 Image.fromarray(out.clip(0, 255).astype(np.uint8)).save(dst) if len(sys.argv) > 3: prev = Image.new("RGBA", (W, H), (40, 60, 80, 255)) prev.alpha_composite(Image.fromarray(out.clip(0, 255).astype(np.uint8))) prev.convert("RGB").save(sys.argv[3]) print("saved", dst)