Skip to content
Related Articles

Related Articles

Improve Article

Finding Difference between Images using PIL

  • Last Updated : 03 Jan, 2021

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-

 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. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

  • 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 noth image is green in colour.




My Personal Notes arrow_drop_up
Recommended Articles
Page :