#!/bin/bash python3 - <<'EOF' import numpy as np, glob 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] TH, TW = T.shape print('target', TW, 'x', TH) fonts = { 'Roboto': '/mnt/d/GIT/aspia_no2fa-3.0/vcpkg4aspia/buildtrees/harfbuzz/src/14.2.1-f76ca539fc.clean/test/subset/data/fonts/Roboto-Regular.ttf', 'SourceSansPro': '/mnt/d/GIT/aspia_no2fa-3.0/vcpkg4aspia/buildtrees/harfbuzz/src/14.2.1-f76ca539fc.clean/test/subset/data/fonts/SourceSansPro-Regular.otf', 'Ubuntu': '/mnt/d/GIT/aspia_no2fa-3.0/vcpkg4aspia/buildtrees/harfbuzz/src/14.2.1-f76ca539fc.clean/test/subset/data/fonts/Ubuntu-Regular.ttf', 'SegoeUI-Semibold': '/mnt/c/Windows/Fonts/seguisb.ttf', 'SegoeUI-Bold': '/mnt/c/Windows/Fonts/segoeuib.ttf', 'SegoeUI': '/mnt/c/Windows/Fonts/segoeui.ttf', } text = 'Kaspersky Rescue Disk 18' def render(path, size): try: f = ImageFont.truetype(path, size) except Exception: return None img = Image.new('L', (700, 100), 255) d = ImageDraw.Draw(img) try: d.text((20, 20), 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 iou(A, B): ha, wa = A.shape; hb, wb = B.shape best = 0 for dy in range(-3, 4): for dx in range(-4, 5): 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 rows = [('ORIGINAL', crop)] for name, path in fonts.items(): best = (0, None, None) for size10 in range(190, 260, 5): size = size10/10.0 B = render(path, size) if B is None: continue if abs(B.shape[1]-TW) > 10: continue s = iou(T, B) if s > best[0]: best = (s, size, B) if best[1]: print(f'{name:18s} size={best[1]:.1f} IoU={best[0]:.3f}') im = Image.new('RGB', (272, 30), (233,233,233)) try: f = ImageFont.truetype(path, best[1]) ImageDraw.Draw(im).text((6, 2), text, font=f, fill=(41,55,84)) except Exception: pass rows.append((f'{name} {best[1]}', im)) scale = 3 out = Image.new('RGB', (272*scale, sum(r[1].height*scale+6 for r in rows)+10), (255,255,255)) y = 5 d = ImageDraw.Draw(out) for name, im in rows: im2 = im.resize((im.width*scale, im.height*scale), Image.LANCZOS) out.paste(im2, (0, y)); d.text((272*scale-200, y+2), name, fill=(0,0,0)) y += im2.height+6 out.save('/path/to/pf/KRD/font-candidates2.png') print('saved') EOF