54 lines
1.9 KiB
Bash
54 lines
1.9 KiB
Bash
#!/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]
|
|
print('target', T.shape)
|
|
|
|
text = 'Kaspersky Rescue Disk 18'
|
|
path = '/path/to/pf/PSD/fonts/Montserrat-SemiBold.ttf'
|
|
|
|
def render(size):
|
|
f = ImageFont.truetype(path, size)
|
|
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 size10 in range(190, 245, 5):
|
|
size = size10/10.0
|
|
B = render(size)
|
|
s = iou(T, B)
|
|
print(f' size={size:<5} w={B.shape[1]:<4} h={B.shape[0]:<3} IoU={s:.3f}')
|
|
if s > best[0]: best = (s, size)
|
|
|
|
print('BEST', best)
|
|
# картинка-сравнение
|
|
im = Image.new('RGB', (272, 30), (233,233,233))
|
|
ImageDraw.Draw(im).text((6, 2), text, font=ImageFont.truetype(path, best[1]), 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
|