97 lines
3.8 KiB
Bash
97 lines
3.8 KiB
Bash
#!/bin/bash
|
|
python3 - <<'EOF'
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
import numpy as np, os
|
|
|
|
FONT = '/path/to/pf/PSD/fonts/Montserrat-VF.ttf'
|
|
OUT = '/path/to/pf/KRD/images'
|
|
os.makedirs(OUT, exist_ok=True)
|
|
|
|
DARK = np.array([41, 53, 84])
|
|
YELL = np.array([234, 224, 115])
|
|
PAGE = np.array([233, 233, 233])
|
|
|
|
items = [
|
|
('legacy-2', '/path/to/pf/PSD/main/main2.bmp', 424, 481),
|
|
('x64-2', '/path/to/pf/PSD/UEFI/x64/main/main2.bmp', 356, 416),
|
|
('x86-2', '/path/to/pf/PSD/UEFI/x86/main/main2.bmp', 424, 481),
|
|
]
|
|
OLD_A = 'Grub2 FileManager (ISO образы)'
|
|
NEW_A = 'Ventoy (ISO образы)'
|
|
OLD_B = 'Kaspersky Rescue Disk 18'
|
|
NEW_B = 'Kaspersky Rescue Disk 24'
|
|
|
|
def font(size=20, wght=550):
|
|
f = ImageFont.truetype(FONT, size)
|
|
f.set_variation_by_axes([wght])
|
|
return f
|
|
|
|
def render(text, color, size=20, wght=550, pad=60):
|
|
f = font(size, wght)
|
|
w, h = 1100, 140
|
|
img = Image.new('RGB', (w, h), tuple(PAGE))
|
|
ImageDraw.Draw(img).text((pad, 40), text, font=f, fill=tuple(int(v) for v in color))
|
|
a = np.array(img).astype(int)
|
|
m = np.abs(a - PAGE).sum(axis=2) > 100
|
|
ys, xs = np.where(m)
|
|
return m[ys.min():ys.max()+1, xs.min():xs.max()+1], xs.min()-pad, ys.min()-40
|
|
|
|
def iou_align(A, B, rng=12):
|
|
ha, wa = A.shape; hb, wb = B.shape; best = (-1, 0, 0)
|
|
for dy in range(-rng, rng+1):
|
|
for dx in range(-rng, rng+1):
|
|
y0 = max(0, dy); x0 = max(0, dx)
|
|
y1 = min(ha, hb+dy); x1 = min(wa, wb+dx)
|
|
if y1 <= y0 or x1 <= x0: continue
|
|
s1 = A[y0:y1, x0:x1]; s2 = B[y0-dy:y1-dy, x0-dx:x1-dx]
|
|
u = np.logical_or(s1, s2).sum()
|
|
if not u: continue
|
|
s = np.logical_and(s1, s2).sum()/u
|
|
if s > best[0]: best = (s, dx, dy)
|
|
return best
|
|
|
|
for name, src, yA, yB in items:
|
|
base = Image.open(src).convert('RGB')
|
|
arr0 = np.array(base).astype(int)
|
|
img = base.copy()
|
|
draw = ImageDraw.Draw(img)
|
|
print('==', name)
|
|
|
|
for tag, y, old, new in (('A', yA, OLD_A, NEW_A), ('B', yB, OLD_B, NEW_B)):
|
|
band = arr0[y-10:y+24, 20:1010]
|
|
# цвет полосы: сэмпл в середине строки
|
|
bar = arr0[y+2, 250]
|
|
# сколько пикселей рядом с тёмным/жёлтым в полосе
|
|
d_dark = np.abs(band - DARK).sum(axis=2) < 150
|
|
d_yell = np.abs(band - YELL).sum(axis=2) < 150
|
|
if d_yell.sum() > 50 and np.abs(bar - YELL).sum() > 100:
|
|
text_color, bar_color = YELL, bar
|
|
elif d_dark.sum() > 50 and np.abs(bar - DARK).sum() > 100:
|
|
text_color, bar_color = DARK, bar
|
|
else:
|
|
# текст того цвета, которого больше и который не совпадает с полосой
|
|
text_color = YELL if d_yell.sum() > d_dark.sum() else DARK
|
|
bar_color = bar
|
|
m = np.abs(band - text_color).sum(axis=2) < 150
|
|
ys, xs = np.where(m)
|
|
if len(xs) == 0:
|
|
print(' ', tag, 'текст не найден'); continue
|
|
x0, x1 = xs.min()+20, xs.max()+20
|
|
y0, y1 = ys.min()+y-10, ys.max()+y-10
|
|
print(f' {tag}: bar={tuple(int(v) for v in bar_color)} text={tuple(int(v) for v in text_color)} bbox=({x0},{y0},{x1},{y1})')
|
|
|
|
rend, rx, ry = render(old, text_color)
|
|
sub = np.abs(arr0[y0-2:y1+3, x0-2:x1+3] - text_color).sum(axis=2) < 150
|
|
s, dx, dy = iou_align(sub, rend)
|
|
print(f' align IoU={s:.3f} dx={dx} dy={dy}')
|
|
px = x0 - 2 + dx - rx
|
|
py = y0 - 2 + dy - ry
|
|
|
|
draw.rectangle([x0-3, y0-4, x1+3, y1+3], fill=tuple(int(v) for v in bar_color))
|
|
draw.text((px, py), new, font=font(), fill=tuple(int(v) for v in text_color))
|
|
print(f' draw "{new}" at ({px},{py})')
|
|
|
|
img.save(f'{OUT}/{name}.png')
|
|
print(' ->', f'{OUT}/{name}.png')
|
|
EOF
|