Open In App

Spot the difference between two images using Python

Last Updated : 24 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to spot differences between two given images using python. In order to perform this task, we will be using the ImageChops.difference() method in Pillow module.

Syntax: ImageChops.difference(image1, image2)

Parameters:

  • image1 first image
  • image2 second image

Return Value: It returns an Image.

Step-by-step Approach:

Step 1: So, today we will be building this magical tool using python and that too with only 8 lines of code. But, before that, we have to install the pillow package of python using this command

pip install pillow

Step 2: Now, after installing this we have to get two images. Make sure that these two images are in the same folder where you’ve kept this python program or else you’ve to provide the path of these images.

Step 3: Call the ImageChops.difference() method with the two images as parameters.

Step 4: Generate the difference between the two images using the show() method.

Implementation:

Input:

Python3




# import module
from PIL import Image, ImageChops
  
# assign images
img1 = Image.open("1img.jpg")
img2 = Image.open("2img.jpg")
  
# finding difference
diff = ImageChops.difference(img1, img2)
  
# showing the difference
diff.show()


Output:

Notice that the output image contains mostly black parts, but some portions of this image are colored. Those colored portions are the spotted differences between the two input images. In this case, the output image shows a total of 6 major differences. 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads