#!/bin/bash python3 - <<'EOF' import numpy as np from PIL import Image, ImageDraw, ImageFont bmp = Image.open('/mnt/f/efi/Paravis/Boot/xor/Main1.bmp').convert('L') crop = bmp.crop((44, 476, 316, 506)) A = np.array(crop) < 128 ys, xs = np.where(A); T = A[ys.min():ys.max()+1, xs.min():xs.max()+1] text = 'Kaspersky Rescue Disk 18' path = '/mnt/c/Windows/Fonts/seguisb.ttf' def render(size, stroke): f = ImageFont.truetype(path, size) img = Image.new('L', (700, 100), 255) d = ImageDraw.Draw(img) d.text((20, 20), text, font=f, fill=0, stroke_width=stroke, stroke_fill=0) m = np.array(img) < 128 ys, xs = np.where(m) return m[ys.min():ys.max()+1, xs.min():xs.max()+1] def iou(A, B): ha, wa = A.shape; hb, wb = B.shape; best = 0 for dy in range(-4, 5): for dx in range(-5, 6): 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 u: best = max(best, np.logical_and(s1, s2).sum()/u) return best best = (-1,) for size10 in range(215, 245, 5): for stroke10 in range(0, 9, 2): size = size10/10.0; stroke = stroke10/10.0 B = render(size, stroke) if abs(B.shape[1]-T.shape[1]) > 8: continue s = iou(T, B) print(f' size={size:.1f} stroke={stroke:.1f} w={B.shape[1]} IoU={s:.3f}') if s > best[0]: best = (s, size, stroke) print('BEST:', best) EOF