PIL, known as the Python Imaging Library, can be used to work with images in a fairly easy way. PIL hasn’t had any changes or development since 2009. Therefore, the kind users of this site have offered to take another look at Pillow. This article will help you learn how to use Pillow.
What is Pillow?
Pillow is a fork of PIL (Python Image Library). It is based on the PIL code, and then transformed into an improved, modern version. Provides support for opening, managing, and saving many image formats. Many things work just like the original PIL.
Downloading and installing Pillow
Before you can start using Pillow, you need to download and install it. Pillow is available for Windows, Mac OS X and Linux. The “latest” version is “8.1.0” and is supported on python 3.6 and above. To install Pillow on Windows computers, use conda or
conda install -c anaconda pillow pip install Pillow
To install Pillow on Linux computers, simply use:
$ sudo pip install Pillow
And to install Pillow on MacOS X, you must first install XCode and then Homebrew. Once Homebrew is installed, use:
$ brew install libtiff libjpeg webp littlecms $ sudo pip install Pillow
Make sure Pillow is installed
Make sure Pillow is installed, open a terminal, and type the following on the current line:
$ python Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from PIL import Image
If the system shows
>>> again, then the Pillow modules are correctly installed.
Before you start using the Pillow module, let’s point out some supported file types:
BMP, EPS, GIF, IM, JPEG, MSP, PCX PNG, PPM, TIFF, WebP, ICO, PSD, PDF. Some file types are read-only, while others are write-only. To see a complete list of supported file types and more information about them, see the Pillow manual.
How to use Pillow to work with images
Since we’re going to work with images, let’s start by downloading one. If you already have an image you want to use, skip this step below. In our example, we will use a standard test image called “Lenna” or “Lena”. This image is used in many image processing experiments. Just go here and download the image. If you click on the image, it will be saved as a 512×512 pixel image.
Let’s take a look at the possible uses of this library. The main functions are in the
Image module. You can create instances of this class in several ways. By loading images from files, processing other images, or creating images from scratch. Import the Pillow modules that you want to use.
from PIL import Image
You will then have access to the functions.
myimage = Image.open(filename) myimage.load()
Loading the image
Use the open method to identify the file on your computer, and then load the identified file with
myfile.load(). Once the image is loaded, you can work with it. The
try except block is often used when working with files. To load an image with
try except use:
from PIL import Image, ImageFilter try: original = Image.open("Lenna.png") except FileNotFoundError: print("File not found")
When we read files from disk with
open(), we don’t need to know the file format. The library automatically detects the format based on the contents of the file. Now that you have an
Image object, you can use the available attributes to inspect the file. For example, if you want to see the size of the image, you can use the
print("Image size:") print(original.format, original.size, original.mode)
size attribute is a
tuple(tuple) containing the width and height (in pixels). The usual
L for grayscale images,
RGB for true color images, and
CMYK for print images. As a result of the code above, you should get the following (if you use “Lenna.png”)
Image size: PNG, (512, 512), RGB
Blurring the image
This example will load and blur the image from your hard drive.
# import the necessary modules from PIL import Image, ImageFilter try: # load the image from the hard drive original = Image.open("Lenna.png") except FileNotFoundError: print("File not found") # blur the image blurred = original.filter(ImageFilter.BLUR) # open the original and blurred image original.show() blurred.show() # save the image blurred.save("blurred.png")
The blurred image should look like this: To open the image, we used the
show() methods. If you don’t see anything, you can install
ImageMagick first and try again.
There is a need to create thumbnails for images. Thumbnails are thumbnail versions of images, but still contain all the most important aspects of the image.
from PIL import Image size = (128, 128) saved = "lenna.jpeg" img = Image.open("lenna.png") img.thumbnail(size) img.save(saved) img.show()
The result of running the script, you can see in the thumbnail:
Filters in Pillow
Pillow module provides the following set of predefined filters to improve the image:
In our final example, we’ll show you how you can apply the
CONTOUR filter to your image. The code below will take our image and apply a filter to it.
from PIL import Image, ImageFilter img = Image.open("Lenna.png") img = img.filter(ImageFilter.CONTOUR) img.save("LennaC" + ".jpg") img.show()
Below you can see the image with the
CONTOUR filter applied: