#!/usr/bin/env python3 """Compara o pixel medio de dois PNGs (palette grossa, suficiente p/ conferir se o icns e o mesmo desenho).""" import struct import sys import zlib def read_png(path): d = open(path, 'rb').read() assert d[:8] == b'\x89PNG\r\n\x1a\n', f'nao e PNG: {path}' pos, w, h = 8, None, None idat = b'' while pos < len(d): ln = struct.unpack('>I', d[pos:pos+4])[0] typ = d[pos+4:pos+8] data = d[pos+8:pos+8+ln] if typ == b'IHDR': w, h = struct.unpack('>II', data[:8]) elif typ == b'IDAT': idat += data pos += 12 + ln if typ == b'IEND': break raw = zlib.decompress(idat) assert w is not None stride = w * 4 + 1 prev = bytearray(w * 4) out = bytearray() for y in range(h): row = bytearray(raw[y*stride:(y+1)*stride]) f = row[0] line = bytearray(row[1:]) for x in range(w): for k in range(4): i = x * 4 + k a = line[i-4] if x > 0 and k < 3 else 0 # aprox: usa vizinho p/ filtros b = prev[i] c = line[i-4] if x > 0 else 0 if f == 1: v = (line[i] + (line[i-4] if x > 0 else 0)) & 255 elif f == 2: v = (line[i] + prev[i]) & 255 elif f == 3: v = (line[i] + ((a + b) >> 1)) & 255 elif f == 4: p = a + b - c pa, pb, pc = abs(p-a), abs(p-b), abs(p-c) pr = a if (pa <= pb and pa <= pc) else (b if pb <= pc else c) v = (line[i] + pr) & 255 else: v = line[i] line[i] = v out += line prev = line return w, h, out def average(path): w, h, px = read_png(path) n = w * h r = sum(px[i] for i in range(0, len(px), 4)) // n g = sum(px[i] for i in range(1, len(px), 4)) // n b = sum(px[i] for i in range(2, len(px), 4)) // n opaque = sum(1 for i in range(3, len(px), 4) if px[i] > 200) * 100 // n return w, h, r, g, b, f'{opaque}% opaco' def main(): icns_png = sys.argv[1] # 1024 extraido do .icns orig_png = sys.argv[2] # PNG original do usuario a = average(icns_png) b = average(orig_png) print(f'icns 1024 -> {a}') print(f'original -> {b}') # Mesmo desenho se cores medias proximas e ambos coloridos/transparentes com padrao similar close = abs(a[2]-b[2]) <= 24 and abs(a[3]-b[3]) <= 24 and abs(a[4]-b[4]) <= 24 print('MATCH (mesmo desenho)' if close else 'DIFF — verificar manualmente') sys.exit(0 if close else 1) if __name__ == '__main__': main()