Files
grass-cutters/Tools/ui/logo_t.py

111 lines
4.6 KiB
Python

"""Replace the scythe standing in for the first T of CUTTERS with a plain T.
py -I Tools/ui/logo_t.py original.png out.png [preview.png]
The originals are the scythe versions in git before 88c1ee9: Assets/Art/UI/logo_grasscutter.png and Docs/Steam/source/logo_clean.png.
Works from the ORIGINAL logo (the one with the scythe). The second T had its top-left corner cut away where the
scythe blade lay over it, so the T is rebuilt symmetric (its right half mirrored onto the left), used for both Ts,
and the dark outline between U and E is drawn again from scratch at the logo's own thickness.
"""
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.float64)
H, W = im.shape[:2]
r, g, b, a = (im[..., i] for i in range(4))
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)
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, hy0, hy1 = xs.min(), ys.min(), ys.max()
row_mid = (hy0 + hy1) / 2
row = sorted((sl[1].start, i + 1, sl[1].stop, sl[0].start, sl[0].stop) for i, sl in enumerate(objs)
if sizes[i] >= 0.002 * H * W and sl[0].start < row_mid < sl[0].stop)
right = [l for l in row if l[0] > hx0]
u = [l for l in row if l[2] < hx0 + 5][-1]
t2, e = right[0], right[1]
print("U", u[0], u[2], "T2", t2[0], t2[2], "E", e[0], e[2])
# every letter of the logo, minus the scythe's pale blade (a white blob sitting between U and T2)
letters = np.zeros_like(white)
for i, sl in enumerate(objs):
if sizes[i] < 0.0005 * H * W:
continue
x0, x1 = sl[1].start, sl[1].stop
if x0 >= u[2] - 2 and x1 <= t2[0] + 40 and sl[0].stop <= t2[4] + 5 and (i + 1) not in (u[1], t2[1]):
continue
letters |= lab == i + 1
# outline thickness: how far the dark band reaches past the letters on the logo's outer edge, away from the scythe
d_let = nd.distance_transform_edt(~letters)
edge = solid & ~nd.binary_erosion(solid)
far = np.ones_like(solid)
far[:, u[0]:e[2]] = False
outline = float(np.median(d_let[edge & far]))
print("outline", outline)
# the T, symmetric about its stem
t2m = lab == t2[1]
y0, y1 = t2[3], t2[4]
low = range(y0 + int((y1 - y0) * 0.6), y1 - 4)
cx = int(round(np.mean([np.nonzero(t2m[y])[0].mean() for y in low if t2m[y].any()])))
half = t2[2] - cx + 3
sym = np.zeros_like(t2m)
px = np.zeros_like(im)
for x in range(cx, cx + half):
m = 2 * cx - x
sym[:, x] = t2m[:, x]
sym[:, m] = t2m[:, x]
px[:, x] = im[:, x]
px[:, m] = im[:, x]
# the new T halfway between U and T2 (same gap on both sides)
gap = (cx - half - u[2] - 2 * half) / 2
dx = int(round(u[2] + gap + half - cx))
newT = np.zeros_like(sym)
newpx = np.zeros_like(px)
newT[:, cx + dx - half:cx + dx + half] = sym[:, cx - half:cx + half]
newpx[:, cx + dx - half:cx + dx + half] = px[:, cx - half:cx + half]
print("stem", cx, "half", half, "new T stem", cx + dx)
# redraw the band from U's right edge to halfway into the T2-E gap
x_lo, x_hi = u[2] + 1, (t2[2] + e[0]) // 2
y_lo, y_hi = max(0, t2[3] - int(outline) - 30), min(H, t2[4] + int(outline) + 30)
R = np.zeros_like(solid)
R[y_lo:y_hi, x_lo:x_hi] = True
keep = letters & ~t2m # other letters' whites (GRASS bottoms, TOGETHER tops, U's edge) as they were
new_white = keep | sym | newT
d = nd.distance_transform_edt(~new_white)
dark_col = np.median(im[dark][:, :3], axis=0)
out = im.copy()
band = R & ~nd.binary_dilation(keep, iterations=2)
cov = np.clip(outline + 0.5 - d, 0.0, 1.0) # outline coverage with a soft 1 px outer edge
out[band, :3] = dark_col
# never less than the logo already covered there (rows close together share one dark mass)
out[band, 3] = np.maximum(cov[band] * 255, a[band])
# a pocket the old handle left between the two stems, now walled in: fill it
pocket = band & nd.binary_fill_holes(out[..., 3] > 128) & (out[..., 3] < 255)
out[pocket, 3] = 255
for mask, pix in ((sym, px), (newT, newpx)):
soft = nd.binary_dilation(mask, iterations=2) & R
al = pix[soft, 3:4] / 255.0
# the copied pixels carry their own anti-aliased edge over the dark outline
out[soft, :3] = pix[soft, :3] * al + dark_col * (1 - al)
out[soft, 3] = 255
res = Image.fromarray(out.clip(0, 255).astype(np.uint8))
res.save(dst)
if len(sys.argv) > 3:
prev = Image.new("RGBA", (W, H), (90, 160, 60, 255))
prev.alpha_composite(res)
prev.convert("RGB").save(sys.argv[3])
print("saved", dst)