75 lines
2.5 KiB
Bash
75 lines
2.5 KiB
Bash
#!/bin/bash
|
|
pip install --quiet pillow numpy >/dev/null 2>&1
|
|
python3 - <<'EOF'
|
|
import numpy as np
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
import glob, os
|
|
|
|
# 1. Кроп надписи с картинки
|
|
bmp = Image.open('/mnt/f/efi/Paravis/Boot/xor/Main1.bmp').convert('L')
|
|
crop = bmp.crop((44, 476, 316, 506)) # с запасом вокруг текста
|
|
a = np.array(crop)
|
|
target = (a < 128) # тёмный текст
|
|
ys, xs = np.where(target)
|
|
print('target ink bbox:', xs.min(), xs.max(), ys.min(), ys.max(), 'pixels:', target.sum())
|
|
|
|
text = 'Kaspersky Rescue Disk 18'
|
|
|
|
def render(font_path, size):
|
|
try:
|
|
f = ImageFont.truetype(font_path, size)
|
|
except Exception:
|
|
return None
|
|
img = Image.new('L', (600, 80), 255)
|
|
d = ImageDraw.Draw(img)
|
|
try:
|
|
d.text((10, 10), text, font=f, fill=0)
|
|
except Exception:
|
|
return None
|
|
m = np.array(img) < 128
|
|
if not m.any():
|
|
return None
|
|
ys, xs = np.where(m)
|
|
return m[ys.min():ys.max()+1, xs.min():xs.max()+1]
|
|
|
|
def score(A, B):
|
|
# A: target crop, B: rendered mask; пробуем сдвиги
|
|
best = -1
|
|
ha, wa = A.shape; hb, wb = B.shape
|
|
for dy in range(-3, 4):
|
|
for dx in range(-3, 4):
|
|
# размещаем B в A со сдвигом
|
|
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
|
|
sub = A[y0:y1, x0:x1]
|
|
subB = B[y0-dy:y1-dy, x0-dx:x1-dx]
|
|
inter = np.logical_and(sub, subB).sum()
|
|
union = np.logical_or(sub, subB).sum()
|
|
if union == 0: continue
|
|
s = inter / union
|
|
if s > best: best = s
|
|
return best
|
|
|
|
# цель обрезаем по чернилам для корректного сравнения
|
|
ys, xs = np.where(target)
|
|
T = target[ys.min():ys.max()+1, xs.min():xs.max()+1]
|
|
|
|
results = []
|
|
fonts = glob.glob('/mnt/c/Windows/Fonts/*.ttf') + glob.glob('/mnt/c/Windows/Fonts/*.otf')
|
|
for fp in fonts:
|
|
for size in (20, 21, 21.63, 22, 23):
|
|
B = render(fp, size)
|
|
if B is None: continue
|
|
if abs(B.shape[1] - T.shape[1]) > 12: # грубый отсев по ширине
|
|
continue
|
|
s = score(T, B)
|
|
if s > 0:
|
|
results.append((s, os.path.basename(fp), size, B.shape))
|
|
|
|
results.sort(reverse=True)
|
|
print('--- top-15:')
|
|
for s, name, size, shape in results[:15]:
|
|
print(f' {s:.3f} {name:32s} size={size:<5} shape={shape}')
|
|
EOF
|