from PIL import Image images = [] for file in range(104): num = str(file).rjust(3, '0') im = Image.open(f'part_{num}.png') px = im.load() images.append((num, im, px)) iter = 0 used = set() for idx1, i1, p1 in images: found = False if idx1 in used: continue for idx2, i2, p2 in images: if idx2 in used: continue good = True for x in range(64): for y in range(64): a = p1[x, y] b = p2[x, y] if a == 0 and b == 0: good = False break if good: found = True for x in range(64): for y in range(64): a = p1[x, y] b = p2[x, y] if b == 0 and a == 255: p1[x, y] = 0 i1.save(f'zhopa/{iter}_{idx1}_{idx2}.png') used.add(idx1) used.add(idx2) iter += 1 break