CV Lab 3
CV Lab 3
OBJECTIVES
Pillow
Pillow is an open source library that has been split from the Python Imaging Library (PIL).
Installation.
Reading an image
To read an image from a jpg or a png file saved on your computer, Pillow's image module
provides a open() function (Image.open). This function returns an image object, which contains
information such as pixel type, image size, and image format. To display the image on your
screen, use the show() function
img = Image.open('E:\\3.jpg')
img.showow()
To write or save an image to a file on your computer, use the save() function associated to the
image object. It takes in the absolute or relative file path to where you want to store the image:
Cropping an image
Cropping an image means to extract a particular region of the image, which is smaller than the
original image. This is sometimes called as a Region of Interest (RoI). The image object has a
crop() function that takes two coordinates--the upper-left corner and the bottom-right corner of
the rectangle that you are interested in--and returns the cropped image:
1
DEPARTMENT OF INFORMATION TECHNOLOGY, QUEST NAWABSHAH
Computer Vision, 21 BS(AI) BATCH
dim=[100,200,600,400]
crop=img.crop(dim)
crop.show()
Grayscale: This is one of the simplest color spaces both in terms of understanding and storing
on a computer. Each pixel value in a grayscale image is a single value between 0 and 255, with 0
representing black and 255 representing white.
img=img.convert('L')
img.show()
Red, Green, Blue (RGB): This is one of the common color spaces that is used in the image
processing world. In a typical RGB image, each pixel is a combination of three values, each
representing a color in red, green, and blue channels.
img=img.convert('RGB') #img=img.convert(mode=’RGB’)
img.show()
Hue, Saturation, Value (HSV): This is a cylindrical coordinate system where we project RGB
values onto a cylinder.
img=img.convert('HSV') #img=img.convert(mode=’HSV’)
img.show()
Geometrical transformation
Resize: To resize an image, use the resize() function, which takes a tuple of the new size as an
argument:
resize=img.resize((500,500))
resize.show()
Rotate: To rotate an image, use the rotate() function, which takes in the degrees to be rotated
(counter clockwise) as an argument:
rot=img.rotate(90)
rot.show()
Image enhancement
Image enhancement involves operations such as changing the contrast, brightness, color balance,
or sharpness of an image. Pillow provides an module, which has ImageEnhance functions that
can perform image enhancement operations.
2
DEPARTMENT OF INFORMATION TECHNOLOGY, QUEST NAWABSHAH
Computer Vision, 21 BS(AI) BATCH
img = Image.open('E:\\3.jpg')
enh=ImageEnhance.Brightness(img)
img=enh.enhance(3)
img.show()
Sometimes for performing tasks such as thresholding we have to access the individual pixels in
an image. Pillow provides a PixelAccess class with functions to manipulate image pixel values.
getpixel() and putpixel() are some of the functions in the PixelAccess class:
getpixel(): This function returns the color value of the pixel at the (x, y) coordinate. It takes a
tuple as an argument and returns a tuple of color values:
img.getpixel((100,100))
putpixel(): This function changes the color value of the pixel at the (x, y) coordinate to a new
color value. Both the coordinates and the new color value are passed as an argument to the
function.
img.putpixel((100,100),(20,30,50,50))
img.getpixel((100,100))