What is Image Processing?
Image processing is any form of processing for which the input is an image or a series of images or videos, such as photographs or frames of video. The output of image processing can be either an image or a set of characteristics or parameters related to the image.
Pillow
Pillow is a Python Imaging Library (PIL), which adds support for opening, manipulating, and saving images. It supports a range of image file formats such as PNG, JPEG, PPM, GIF, TIFF and BMP.
Let’s start with the basics
First install pillow using command
pip install pillow
Let’s see how we can read an image and display an image using pillow and python.
1) Reading and Displaying an Image
Let’s see the syntax for reading an image in our IDE.
First you have to import the PIL library and from that library import Image module. You can open an image by using open () method inside the parenthesis give the address of the image. By using show () method you can see the output
Syntax
from PIL import Image img = Image.open("C:/Users/Desktop/cute_baby.jpg") img. show ()
Output
You can get some details about the image using object attributes
a) To get the format of the image
print (img. format)
which is JPEG in this case.
b) The pixel format used by the image. Generally, values are “1”,” L”, “RGB” or “CMYK
print (img. mode)
The output here is RGB
c) The image size which is in pixels. The size is given as 2 tuple representing width and height
print (img. size)
Here the size of the image is (2000, 1500)
2) Resizing and Saving an image
As we have seen the current image size is (2000, 1500) we can resize it according to our convenience using a method called resize ()
You can also save your edited image into your pc using save () method. Inside the parenthesis provide the location where you want to save the image.
If you want to resize it by keeping the aspect ratio same you can use a method called thumbnail (). Let’s see how we can implement this using code
Syntax
from PIL import Image img = Image.open("C:/Users/Desktop/cute_baby.jpg") print (img. format) small_img = img. resize ((1200,715)) small_img1 = small_img. save("C:/Users/Desktop/baby.jpg") img. thumbnail ((1200,715)) img. save("C:/Users/suyog/Desktop/thumbnail.jpg") small_img. show () img. show ()
Output
Original Image
Resized image
Thumbnail image
3) Cropping and saving an image
As far we have seen how to resize an image. Let’s see how we can crop an image using pillow library. There is a method for cropping an image called crop ()
Let’s see how we can implement it using code
Syntax
from PIL import Image img = Image.open("C:/Users/Desktop/cute_baby.jpg") cropped_img = img. crop ((0,0, 1300,1300)) cropped_img. save("C:/Users/Desktop/crop.jpg")
Output
4) Pasting one image on another
If you want to put one image on another at a specific location you can do that using paste () method
Let’s see how we can implement it using code
Syntax
from PIL import Image img1 = Image.open("C:/Users/Desktop/cute_baby.jpg") print (img1.size) img2 = Image.open("C:/Users/Desktop/monkey.jpg") print (img2.size) img2.thumbnail((500,400)) img1.paste(img2, (50,50)) img1.save("C:/Users/Desktop/new.jpg")
Output
In this way we have learn some basics of Image processing using Pillow