Python Pillow PIL

Older version of these notes is here

https://pillow.readthedocs.io/en/stable/


After you manage to import PIL successfully, you are ready to play with an image in Python.

Some Basics

You will create an object for your image. An object is like a variable, but it has its own set of variables and functions.

from PIL import Image
img = Image.open('cat.jpg')

This code has created an image objected named img.

You need to know a few key attributes (variables) that belong to this object.

Try something like this:

print(img.width)
print(img.height)
print(img.mode)

Width and height tell you  how how many pixes wide and tall your image is.

The mode of an image tells you what sort of color data it contains.

Example modes:
“RGB” –  red/green/blue color image
“L” – lightness – grayscale image
“RGBA” – RGB plus Alpha (“A”) channel – opacity/transparency

Modes notes are here
Modes in official documentation are here

You can convert one mode to another. Here’s how you can make a grayscale image from an RGB image:

gray_img = img.convert('L')

Read about Image.convert() here

Note: the convert() function returns a new object.

This means the following command won’t do anything:

img.convert('L')
# nothing happens; img is still a color image object.
# original image is unchanged.

Fix that by making a new image; see “gray_img = ” in the previous example.

Resize

You can easily resize an image.

Click here to read about Image.resize() function

The resize function returns a new copy of the object, so you will need to do something like this:

img = Image.open('cat.jpg')
small = img.resize((50,50))

Now you have two Image objects: the original (img) and a smaller one that’s 50×50 pixels (small).

Notice that (x,y) sizes have an extra set of parentheses. They go into the function as a single tuple, which is like a list.

Crop

You can easily crop an image. “Crop” means picking a rectangle of the image you want to keep and removing the rest.

Example: if we have a 1000×500 image, here’s how to crop 100 pixels off around the outsides.

img = Image.open('cat.jpg')
cropped = img.crop((100,100,900,400))

This defines a rectangle inside the original image starting at (100,100), inside the top left corner, and extending to (900,400), inside the bottom right corner.

Click here to read about Image.crop() function

Create New Blank Image

You can create a blank new image.

You need to specify the size, mode, and color of the new image.
Example creating a red 600×400 image:

red_bg = Image.new('RGB', (600,400), color='red')

Click here to read about Image.new() function

Save Image File

You can save any image object as a file. For example, add this to the red background image example above.

red_bg.save('red.png')

The filename you enter must end in a valid image type file extension such as “.png” or “.jpg” or “.bmp” or “.gif”.

Note: “jpg” is a compressed format that will change your pixel data, so you might want to avoid that for our purposes. “png” is a good format to use.

Read about the Image.save() function here