ICT-4202, DIP Lab Manual - 3
ICT-4202, DIP Lab Manual - 3
Jahangirnagar University
Savar, Dhaka-1342
Lab Manual
Lab No.: 3
Lab Title: IMAGE PROCESSING USING PILLOW LIBRARY OF
PYTHON
Prepared by
Mehrin Anannya
Assistant Professor
Institute of Information Technology
Lab Contents:
● Introduction to PILLOW library
● Image Works using Pillow library
● Reading multiple images using pillow library
● Convert OpenCV image to PIL image in Python
● Additive and subtractive color
● Color spaces in RGB
#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.
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.
img.show();
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
print(img.mode)
B) Getting the size of the image: This attribute provides the size of the image. It returns a tuple
that contains width and height.
print(img.size)
C) Getting the format of the image: This method returns the format of the image file.
print(img.format)
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)
r_img = img.rotate(40)
r_img.show()
vertical_img.show()
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
Syntax:
PIL.Image.crop(box = None)
Parameters:
box: a 4-tuple defining the left, upper, right, and lower pixel coordinate.
import cv2
from PIL import Image
image = cv2.imread('Images\Flower.jpeg')
B, G, R = cv2.split(image)
# Corresponding channels are separated
cv2.imshow("original", image)
cv2.waitKey(0)
cv2.imshow("blue", B)
cv2.waitKey(0)
cv2.imshow("Green", G)
cv2.waitKey(0)
cv2.imshow("red", R)
cv2.waitKey(0)
cv2.destroyAllWindows()