Python: Pillow (a fork of PIL)

03 Jul, 2021
Python Imaging Library (expansion of PIL) is the de facto image processing package for Python language. It incorporates lightweight image processing tools that aids in editing, creating and saving images. Support for Python Imaging Library got discontinued in 2011, but a project named pillow forked the original PIL project and added Python3.x support to it. Pillow was announced as a replacement for PIL for future usage. Pillow supports a large number of image file formats including BMP, PNG, JPEG, and TIFF. The library encourages adding support for newer formats in the library by creating new file decoders.This module is not preloaded with Python. So to install it execute the following command in the command-line: 
pip install pillow
Note: Several Linux distributions tend to have Python and PIL preinstalled into them.
1. Opening an image using open(): The PIL.Image.Image class represents the image object. This class provides the open() method that is used to open the image.
Example: Let’s suppose the image is:
Python3
from PIL import Image  # test.png => location_of_imageimg = Image.open(r"test.png")  
Note: Location of image should be relative only if the image is in the same directory as the Python program, otherwise absolute (full) path of the image should be provided. 
2. Displaying the image using show(): This method is used to display the image. For displaying the image Pillow first converts the image to a .png format (on Windows OS) and stores it in a temporary buffer and then displays it. Therefore, due to the conversion of the image format to .png some properties of the original image file format might be lost (like animation). Therefore, it is advised to use this method only for test purposes.
Python3
from PIL import Image  img = Image.open(r"test.png")img.show()
Output: 
3. Obtaining information about the opened image
A) Getting the mode (color mode) of the image: The mode attribute of the image tells the type and depth of the pixel in the image. A 1-bit pixel has a range of 0-1, and an 8-bit pixel has a range of 0-255. There are different modes provided by this module. Few of them are:
Example:
Python3
from PIL import Image  img = Image.open(r"test.png")print(img.mode)
Output:
RGBA
Note: Refer to the documentation to know about the modes.
B) Getting the size of the image: This attribute provides the size of the image. It returns a tuple that contains width and height.
Example:
Python3
from PIL import Image  img = Image.open(r"test.png")print(img.size)
Output:
(180, 263)
C) Getting the format of the image: This method returns the format of the image file.
Python3
from PIL import Image  img = Image.open(r"test.png")print(img.format)
Output:
PNG
4. Rotating an image using rotate(): After rotating the image, the sections of the image having no pixel values are filled with black (for non-alpha images) and with completely transparent pixels (for images supporting transparency)
Example:
Python3
from PIL import Image  angle = 40img = Image.open(r"test.png")r_img = img.rotate(angle)
Output:
5. Resizing an image using resize(): Interpolation happens during the resize process, due to which the quality of image changes whether it is being upscaled (resized to a higher dimension than original) or downscaled (resized to a lower Image then original). Therefore resize() should be used cautiously and while providing suitable value for resampling argument.
Example:
Python3
from PIL import Image  size = (40, 40)img = Image.open(r"test.png")r_img = img.resize(size) r_img.show()
Output:
6. Saving an image using save(): While using the save() method Destination_path must have the image filename and extension as well. The extension could be omitted in Destination_path if the extension is specified in the format argument.
Python3
from PIL import Image  size = (40, 40)img = Image.open(r"test.png")r_img = img.resize(size, resample = Image.BILINEAR) # resized_test.png => Destination_pathr_img.save("resized_test.png") # Opening the new imageimg = Image.open(r"resized_test.png")print(img.size)
Output:
(40, 40)

 
VasuDev4
sagar0719kumar
python-modules
Python-pil
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate over a list in Python
How to iterate through Excel rows in Python?
Enumerate() in Python
Python Dictionary
Python OOPs Concepts
Different ways to create Pandas Dataframe
*args and **kwargs in Python
Python Classes and Objects
Introduction To PYTHON
Stack in Python