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:
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:
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: