Open In App
Related Articles

Finding Difference between Images using PIL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Python interpreter in itself doesn’t contain the ability to process images and making out a conclusion to it. So, PIL(Python Imaging Library) adds image processing powers to the interpreter. PIL is an open-source library that provides python with external file support and efficiency to process images and their representations. Basically, PIL is designed to access data in the form of images (pixels) to make the analysis faster.

PIL supports image formats like-

  • jpeg
  • tiff
  • png
  • jpg
  • gif

There are a lot of functions that can be performed using PIL, they are-

1) Uploading images

Using PIL, we can load an image and display it. 

Code: After installing the PIL library, run the following code to display any image say abc

Python3

from PIL import Image
 
 
img1 = Image.open('abc.jpg')
img1.show()

                    

Output:

The image will display like this after running the code

2) Saving images

Code: For saving image 

Python3

from PIL import Image
 
 
img1 = Image.open('flower.png')
img1.save('flower.png')

                    

Note: Other functions using PIL – Image processing, difference using ImageChops, downloading, Reading pixels, etc.

Finding the Difference between two images using PIL library

To find the difference, upload 2 images in the interpreter and then using ImageChops find the difference between both of them, output will be self-explanatory.

Images used for difference:

r.jpg

p.jpg

Python3

from PIL import Image, ImageChops
 
 
img1 = Image.open('p.jpg')
img2 = Image.open('r.jpg')
 
diff = ImageChops.difference(img1, img2)
 
if diff.getbbox():
    diff.show()

                    

Output: 

The extra portions / difference between both image is green in colour.



Last Updated : 14 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads