#!/bin/bash python3 - <<'EOF' import numpy as np from PIL import Image, ImageDraw, ImageFont path = '/path/to/pf/PSD/fonts/Montserrat-VF.ttf' 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] print('target', T.shape) text = 'Kaspersky Rescue Disk 18' def render(size, wght): f = ImageFont.truetype(path, size) try: f.set_variation_by_axes([wght]) except Exception as e: print('var err', e); return None img = Image.new('L', (700, 100), 255) ImageDraw.Draw(img).text((20, 20), text, font=f, 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 = (0,) for wght in (500, 550, 600, 650, 700): for size10 in range(190, 240, 5): size = size10/10.0 B = render(size, wght) if B is None: continue s = iou(T, B) if abs(B.shape[1]-T.shape[1]) < 8: print(f' w={wght} size={size:<5} w_px={B.shape[1]:<4} IoU={s:.3f}') if s > best[0]: best = (s, size, wght) print('BEST', best) s, size, wght = best im = Image.new('RGB', (272, 30), (233,233,233)) f = ImageFont.truetype(path, size); f.set_variation_by_axes([wght]) ImageDraw.Draw(im).text((6, 2), text, font=f, fill=(41,55,84)) orig = Image.open('/mnt/f/efi/Paravis/Boot/xor/Main1.bmp').convert('RGB').crop((44, 476, 316, 506)) out = Image.new('RGB', (272*3, 30*3*2+20), (255,255,255)) out.paste(orig.resize((orig.width*3, orig.height*3), Image.LANCZOS), (0,0)) out.paste(im.resize((im.width*3, im.height*3), Image.LANCZOS), (0,30*3+10)) out.save('/path/to/pf/KRD/montserrat-check.png') print('saved') EOF