Skip to content

Instantly share code, notes, and snippets.

@michaelneu
Created March 8, 2019 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelneu/6f3596f514507e762ba50a3870d33066 to your computer and use it in GitHub Desktop.
Save michaelneu/6f3596f514507e762ba50a3870d33066 to your computer and use it in GitHub Desktop.
Graphically diff similar images
#!/usr/bin/env python3
from PIL import Image
def diff(image_a, image_b):
pixels_a = image_a.load()
pixels_b = image_b.load()
width_a, height_a = image_a.size
width_b, height_b = image_b.size
if width_a != width_b or height_a != height_b:
print("unequal sizes")
return None
diff = image_b.copy()
diff_pixels = diff.load()
diffed = False
for x in range(width_a):
for y in range(height_a):
pixel_a = pixels_a[x, y]
pixel_b = pixels_b[x, y]
if pixel_a != pixel_b:
diffed = True
diff_pixels[x, y] = (255, 0, 0)
if diffed:
return diff
return None
if __name__ == "__main__":
for i in range(1, 24):
a = Image.open("a-%d.jpg" % i)
b = Image.open("b-%d.jpg" % i)
result = diff(a, b)
if result:
print("differences found on page", i)
result.save("diff-%d.png" % i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment