Skip to content

Instantly share code, notes, and snippets.

@Flushot
Created August 31, 2016 17:49
Show Gist options
  • Save Flushot/64cdb9351d1474c91c0e449e6bc0f37b to your computer and use it in GitHub Desktop.
Save Flushot/64cdb9351d1474c91c0e449e6bc0f37b to your computer and use it in GitHub Desktop.
from PIL import ImageChops, ImageDraw
def create_diff_image(img1, img2):
"""
Compares two Images and either creates an Image with differences
outlined with red bounding boxes, or returns None if no differences
were found.
"""
diff_img = img2.copy()
differences = ImageChops.differences(img1, img2)
bounding_box = differences.getbbox()
if bounding_box is None:
diff_img.close()
return None
else:
draw = ImageDraw.Draw(diff_img)
draw.rect(bounding_box, outline=(255, 0, 0))
return diff_img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment