# Где лежат все Singles папки на листе (по сетке 16): locate.py <лист.png> <папка Singles> [фильтр-подстрока]
# Печатает: имя  размер  [col,row] (клетки) — первое точное совпадение по сетке 16 px.
import sys, os
import numpy as np
from PIL import Image

sheet = np.asarray(Image.open(sys.argv[1]).convert('RGBA'))
flt = sys.argv[3] if len(sys.argv) > 3 else ''
H, W = sheet.shape[:2]

def norm(a):
    a = a.copy(); a[a[:, :, 3] == 0] = 0; return a.tobytes()

def key(n):
    import re
    return [int(t) if t.isdigit() else t for t in re.split(r'(\d+)', n)]

for f in sorted(os.listdir(sys.argv[2]), key=key):
    if flt not in f or not f.endswith('.png'): continue
    t = np.asarray(Image.open(os.path.join(sys.argv[2], f)).convert('RGBA'))
    th, tw = t.shape[:2]; tb = norm(t); hit = None
    for y in range(0, H - th + 1, 16):
        for x in range(0, W - tw + 1, 16):
            if norm(sheet[y:y + th, x:x + tw]) == tb: hit = (x // 16, y // 16); break
        if hit: break
    name = f.split('16x16_', 1)[-1][:-4]
    print(f'{name:28s} {tw}x{th} {hit}')
