Pillow or PIL¶
The Python Image Library (PIL), although not in the standard library, has been Python’s best-known 2-D image processing library. It predated installers such as pip, so a “friendly fork” called Pillow was created. Although the package is called Pillow, you import it as PIL to make it compatible with the older PIL.
Pillow’s documentation is good, check it out here: https://pillow.readthedocs.io/en/stable/
Set-up and install¶
As usual, a virtualenv is created and activated to do our work in.
Pillow can be installed on most platforms and has been tested for the latest Python version (3.7) across the most commonly used platforms. Simply install Pillow with pip:
code
(venv) $ pip install pillow
Let’s begin…¶
Let’s cover off some basic usage of Pillow; opening Images, resizing and saving images.
If you want to follow along, find an image that you want to use and keep it handy in the same working directory of your program.py.
1) Opening (and saving) images¶
# program.py
from PIL import Image
# to load an image from a file, use the open() function in the image module. Note the capitalisation of image here.
image = Image.open('images/original.jpg')
It’s not unreasonable to have expected that to ‘open’ on your screen, but that’s not what is meant here. You can test if the opening of the image in Python was successful by ensuring it returned an image object:
# if successful, should return an Image object:
print(type(image)) # output: