point_table = [0] + [255] * 255

def black_or_b(a, b):
    diff = ImageChops.difference(a, b)
diff = diff.convert('L')
diff = diff.point(point_table)
new = diff.convert('RGB')
new.paste(b, mask=diff)
return new
a = Image.open('a.png')
b = Image.open('b.png')
c = black_or_b(a, b)
c.save('c.png')
Comparing two images pixel-wise with PIL (Python Imaging Library)