Python Imaging Library (PIL) is an open source image processing and manipulation library. It worked well with multiple image formats of images and its ease of use. It is obvious that we can use Image processing tools like Corel Draw and Photoshop.
But when it comes to automate things we need something reliable. Python Imaging Library helped users in Image Manipulation tasks. But it never got new updates after 2009 as per their official site timeline.
With the regular updates of Python language it became crucial for something to take the place of PIL. So, Alex Clark with Some Contributors forked Python Imaging Library into Pillow Library.
Now with the help of Pillow library Image Manipulation became an easy task. It allowed multiple manipulation features to be applied on Images having various formats. The main thing that made it more helpful was its support with Python 3.
In this article we will be using Pillow Library to work on Image Processing and Manipulation. We will move through the topics step by step with real world examples.
Different Modules in Pillow Library
ImageDraw: This Module can be used for creating simple 2D graphics for the Image object. It also helps in draw over other pictures to retouch them easily.
ImageColor: ImageColor module contains colors in different format arranged in a tabular manner. It also contains the converters to convert from one one format to other.
ImageEnhance: This module can enhance the image by applying different classes that are predefined. One of the most useful module for Image manipulation.
ImageFont: It is used to store and apply different bitmap fonts while using ImageDraw .
ImageFilter: ImageFilter module is used to apply different filter functions on Images .
ImageFile: This module is used for opening and closing image files through Pillow Library.
Capabilities of Pillow Library
The earlier used Python Imaging Library was efficient enough for Image programming. One reason for starting using its successor Pillow was that PIL stopped getting updates. But then users found Pillow an amazing replacement for PIL and it is still working at the best pace.
Here are some of the features or capabilities of Pillow Library:
- Changing Format of Images
- Cropping Images
- Applying Filter to Images
- Resizing Images
- Pasting One Image on Another
- Flipping Pictures
- Drawing on Images
Image Class for Working
Image is the major class in Pillow library in which all the image manipulations tasks are performed. So before performing any manipulation on image, you need to create an instance of Image class by loading the file in it.
Implementation of Pillow Library
You might have got enough information about this amazing Python Library. Now we shall be moving on to code and test the capabilities on our own. Without coding you will never be able to enhance your programming skills. Open up your laptop and move through the below points steps by step.
We are using Jupyter Notebook but you can use any Python IDE for working.
1. Installing Library
There is an important warning about installing Pillow. The official site warns that both PIL and Pillow can’t be used together. So you might need to uninstall Python Imaging Library before installing Pillow.
Firstly Download the Library from here: Download
For installing it just type down the code:
pip install Pillow
2. Opening Image
Image files can be loaded and opened in the environment with the following code:
from PIL import Image im = Image.open("img.jpeg")
3. Checking Image Attributes
We can check different image attributes related to the image we just opened.
format= type of image (JPEG,PNG etc.)
size= size of image
mode= mode of opening (RGB)
Use the following code to check image attributes:
print(im.format, im.size, im.mode)
4. Showing Image
To show image in a separate window you can use the follow code:
5. Cropping Image
Cropping is one of the most important process in image manipulation. It is widely used process in web applications even while uploading profile pictures. Pillow allows us to crop images efficiently.
Here is the code for it:
box = (100, 100, 400, 400) region = im.crop(box)
The box attribute here has 4 parameters as the length of four sides of image.
6. Implementing Some Functions
a) Resizing Image
Images can be easily resized using the im.resize function. It has two parameters on the basis of which it works. The code for it is given below:
out = im.resize((128, 128))
b) Rotating Image
out = im.rotate(45)
Transpose has many other sub functions to apply. They can be used to manipulate images in multiple ways. Let us see how it can be used to Flip images through sides.
out = im.transpose(Image.FLIP_LEFT_RIGHT)
It brings the left part of image to the right and vice versa.
out = im.transpose(Image.FLIP_TOP_BOTTOM)
It brings the top part to the bottom and vice versa.
out = im.transpose(Image.ROTATE_90)
out = im.transpose(Image.ROTATE_180)
d) Converting Color Format
It is used to change the color of image
out = im.convert("L")
7. Working with Image Filters
There are multiple image filters provided by Pillow to enhance images. We have used and shown some of them below:
Used for Blurring images
blurred_image = im.filter(ImageFilter.BLUR)
b) Gaussian Blur
Another filter for blurring images
blurred_image = im.filter(ImageFilter.GaussianBlur)
c) Box Blur
It is another Blur filter to apply on pictures
blurred_image = im.filter(ImageFilter.BoxBlur(9))
8. Enhancing Images
The below code can be used to Enhance your images by applying multiple classes.
from PIL import ImageEnhance enh = ImageEnhance.Contrast(im) enh.enhance(1.3)
9. Converting image Type
As we have already mentioned that Pillow supports a large variety of file formats for image. It also has an amazing feature through which an image can be converted from one format to another.
converted = Image.open("img.png") converted.save('converted.jpeg', 'jpeg')
In the above code we have converted from PNG format to JPEG format. You can use it to convert from any format to the other.
10. Drawing Images
Until now we were only dealing with a pre-built image . Now its time to create an image of our own. Pillow also provides you way through which you can create image by drawing on a blank page.
from PIL import Image, ImageDraw image = Image.new('RGBA', (100, 100), 'white') drawing = ImageDraw.Draw(image) drawing.rectangle((5, 5, 50, 50), fill='red') image.save('drawing.png')
So this was all about the Introduction of Pillow Library for Image Manipulation. Python is always at our rescue and here too Python made it amazingly easy. Hope this guide would have helped you in learning something new.
Here is the link of Git for further code segments: Varsha Saini’s Github
Do comment down if you have any doubts regarding Pillow and its functions.