#!/bin/bash python3 - <<'EOF' from PIL import Image, ImageDraw, ImageFont import numpy as np, os FONT = '/path/to/pf/PSD/fonts/Montserrat-VF.ttf' OUT = '/path/to/pf/KRD/images' os.makedirs(OUT, exist_ok=True) DARK = np.array([41, 53, 84]); YELL = np.array([234, 224, 115]); PAGE = np.array([233, 233, 233]) TOL = 55 items = [ ('legacy-1', '/path/to/pf/PSD/main/main1.bmp', 424, 481), ('legacy-2', '/path/to/pf/PSD/main/main2.bmp', 424, 481), ('x64-1', '/path/to/pf/PSD/UEFI/x64/main/main1.bmp', 356, 416), ('x64-2', '/path/to/pf/PSD/UEFI/x64/main/main2.bmp', 356, 416), ('x86-1', '/path/to/pf/PSD/UEFI/x86/main/main1.bmp', 424, 481), ('x86-2', '/path/to/pf/PSD/UEFI/x86/main/main2.bmp', 424, 481), ] OLD_A, NEW_A = 'Grub2 FileManager (ISO образы)', 'Ventoy (ISO образы)' OLD_B, NEW_B = 'Kaspersky Rescue Disk 18', 'Kaspersky Rescue Disk 24' def font(size=20, wght=550): f = ImageFont.truetype(FONT, size); f.set_variation_by_axes([wght]); return f def render(text, color, size=20, wght=550, pad=60): img = Image.new('RGB', (1100, 140), tuple(PAGE)) ImageDraw.Draw(img).text((pad, 40), text, font=font(size, wght), fill=tuple(int(v) for v in color)) a = np.array(img).astype(int) m = np.sqrt(((a - PAGE) ** 2).sum(axis=2)) > TOL ys, xs = np.where(m) return m[ys.min():ys.max()+1, xs.min():xs.max()+1], xs.min()-pad, ys.min()-40 def iou_align(A, B, rng=12): ha, wa = A.shape; hb, wb = B.shape; best = (-1, 0, 0) for dy in range(-rng, rng+1): for dx in range(-rng, rng+1): 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 not u: continue s = np.logical_and(s1, s2).sum()/u if s > best[0]: best = (s, dx, dy) return best for name, src, yA, yB in items: base = Image.open(src).convert('RGB') arr0 = np.array(base).astype(int) img = base.copy(); draw = ImageDraw.Draw(img) print('==', name) for tag, y, old, new in (('A', yA, OLD_A, NEW_A), ('B', yB, OLD_B, NEW_B)): bar = arr0[y+2, 250] band = arr0[y-10:y+24, 20:470] # только зона текста (справа графика) cnt = {} for nm, col in (('dark', DARK), ('yellow', YELL)): cnt[nm] = int((np.sqrt(((band - col) ** 2).sum(axis=2)) < TOL).sum()) # выбираем цвет текста: тот, что не совпадает с полосой и его достаточно if np.sqrt(((bar - YELL) ** 2).sum()) < TOL and cnt['dark'] > 30: text_color, bar_color = DARK, bar elif np.sqrt(((bar - DARK) ** 2).sum()) < TOL and cnt['yellow'] > 30: text_color, bar_color = YELL, bar else: text_color = YELL if cnt['yellow'] >= cnt['dark'] else DARK bar_color = PAGE if cnt['yellow'] < 30 and cnt['dark'] < 30 else bar m = np.sqrt(((band - text_color) ** 2).sum(axis=2)) < TOL ys, xs = np.where(m) if len(xs) == 0: print(' ', tag, 'текст не найден'); continue # колонки с чернилами, группируем по разрывам; последняя короткая группа = хоткей colsum = m.sum(axis=0) cols = np.where(colsum >= 2)[0] groups = [] if len(cols): s = cols[0]; prev = cols[0] for c in cols[1:]: if c - prev > 12: groups.append((s, prev)); s = c prev = c groups.append((s, prev)) if len(groups) > 1 and (groups[-1][1] - groups[-1][0]) < 60: groups = groups[:-1] if not groups: print(' ', tag, 'текст не найден'); continue gx0, gx1 = groups[0][0], groups[-1][1] x0, x1 = gx0+20, gx1+20 rowsub = m[:, gx0:gx1+1] rowsum = rowsub.sum(axis=1); rows = np.where(rowsum >= 2)[0] y0, y1 = rows.min()+y-10, rows.max()+y-10 print(f' {tag}: bar={tuple(int(v) for v in bar_color)} text={tuple(int(v) for v in text_color)} bbox=({x0},{y0},{x1},{y1})') rend, rx, ry = render(old, text_color) sub = np.sqrt(((arr0[y0-2:y1+3, x0-2:x1+3] - text_color) ** 2).sum(axis=2)) < TOL s, dx, dy = iou_align(sub, rend) px = x0 - 2 + dx - rx; py = y0 - 2 + dy - ry print(f' align IoU={s:.3f} dx={dx} dy={dy} -> draw at ({px},{py})') draw.rectangle([x0-3, y0-4, x1+3, y1+3], fill=tuple(int(v) for v in bar_color)) draw.text((px, py), new, font=font(), fill=tuple(int(v) for v in text_color)) img.save(f'{OUT}/{name}.png') print(' ->', f'{OUT}/{name}.png') EOF