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)
- image1 first image
- image2 second image
Return Value: It returns an Image.
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.
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.