Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython: Pillow (a fork of PIL)

Python: Pillow (a fork of PIL)

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.

Beginning with Pillow

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_image
img = 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:
 

python-pil-1

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:

Mode Description
1 1-bit pixels, black and white
L 8-bit pixels, Greyscale
P 8-bit pixels, mapped to any other mode using a color palette
RGB 3×8-bit pixels, true color
RGBA 4×8-bit pixels, true color with transparency mask

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 = 40
img = Image.open(r"test.png")
r_img = img.rotate(angle)


Output:

python-pil-2

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:

python-pil-3

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_path
r_img.save("resized_test.png")
 
# Opening the new image
img = Image.open(r"resized_test.png")
print(img.size)


Output:

(40, 40)

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments