Finding Difference between Images using PIL


In image processing, finding the difference between two images is a crucial step in various applications. It is essential to understand the difference between the two images, which can help us in detecting changes, identifying objects, and other related applications. In this blog, we will explore how to find the difference between two images using the Python Imaging Library (PIL).

Installation and Syntax

To use PIL, we need to install it using the pip package manager. We can install PIL by running the following command in the terminal −

pip install pillow

To find the difference between two images using PIL, we can use the ImageChops module. The ImageChops module provides various operations on images, including finding the difference between two images. The syntax for finding the difference between two images using PIL is as follows −

from PIL import Image, ImageChops

# find the difference
diff = ImageChops.difference(img1, img2)

# show the difference
diff.show()

Algorithm

  • Import all necessary modules, such as the ImageChops module for image comparison and the PIL module for image processing.

  • The Image.open() function, which is offered by the PIL module, should be used to open the two pictures that need to be compared.

  • To determine the difference between the two pictures, use the ImageChops.difference() function. A new image object representing the absolute value of the pixel-by-pixel difference between the two input pictures is returned by this function after receiving two image objects as input.

  • The final image will be in grayscale, with white pixels denoting areas of difference between the two photos, and black pixels denoting no difference.

  • Using the show() function offered by the Image module, display the difference picture. This will reveal the different image in a new window.

  • The Image module's save() method is an optional way for you to save the various pictures to a file. This method saves the picture to the chosen file using the file name and format as input.

Note − Keep in mind that the size and resolution of the two images being compared should be the same, otherwise, the difference image might not accurately represent the differences between the two images.

Download and rename the following images as beach.jpg and beach2.jpg

Example 1

from PIL import Image, ImageChops

# open images
img1 = Image.open('image1.jpg')
img2 = Image.open('image2.jpg')

# find the difference
diff = ImageChops.difference(img1, img2)

# show the difference
diff.show()

This code opens two images, image1.jpg and image2.jpg, and finds the difference between them using the ImageChops.difference() method. It then displays the different image using the show() method.

Example 2

from PIL import Image, ImageChops

# open images
img1 = Image.open('beach.jpg')
img2 = Image.open('beach2.jpg')

# resize images
img1 = img1.resize((400, 400))
img2 = img2.resize((400, 400))

# find the difference
diff = ImageChops.difference(img1, img2)

# threshold the difference image
threshold = 50
diff = diff.point(lambda x: 0 if x < threshold else 255)

# show the difference
diff.show()

This code opens two images, image1.png and image2.png, and resizes them to a size of 400x400. It then finds the difference between the two images using the ImageChops.difference() method. The difference image is then thresholded to a value of 50 using the point() method. Finally, the difference image is displayed using the show() method.

Applications

  • Changes in observation movies or photos can be found by looking at the distinctions between pictures.

  • It can also be used to discover and emphasize visual changes in image processing applications, such as those used in medical imaging to locate tumors or other anomalies.

  • In forensic investigations, where comparing surveillance footage can help reveal suspects or uncover new evidence, this method is especially useful.

  • Also, it tends to be utilized in modern applications to screen hardware and pinpoint any shifts or breakdowns.

Conclusion

In this lesson, we learnt how to use Python's Python Imaging Library (PIL) to determine the differences between two photos. Installing PIL and importing the required modules was the first step. Then, two approaches to determining the difference between two photos were discussed - The first technique was using the function ImageChops.difference(), which is a quick and easy approach to determine the difference between two pictures and the second technique included subtracting the pixel values from the two photos using NumPy to create a new image that highlighted the discrepancies. We concluded by talking over some potential uses for picture difference detection.

Updated on: 21-Aug-2023

918 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements