PIP-Talk - Week 45

PIP-Talk - Week 45!

Every week going forward I plan to write a little about interesting modules/libraries available for Python.

The topic for this week is: Pillow

If you looking for a module/library to work with images, you will find this piptalk especially interesting.

Pillow provides image processing features that are similar to ones found in image processing software such as Photoshop. Pillow is often the preferred option for high-level image processing tasks that don’t require more advanced image processing expertise. It’s also often used for exploratory work when dealing with images.

Pillow aka “The Python Imaging Library” is ideal for image archival and batch processing applications. You can use the library to create thumbnails, convert between file formats, print images, image resizing, rotation etc.


Background and a Tribute to a friend

Pillow is built on top of PIL (Python Image Library).

PIL was the most important module for image processing in early days of Python. It was originally developed by a friend and a former colleague of mine: Fredrik Lundh. Fredrik was a early python core developer and the father of the: Elementtree and the re modules in the Python standard lib and later also a member of the Youtube team at Google.

Fredrik tragically passed away a year ago (December 2021) and Guido wrote a note about Fredrik’s contributions to Python and the Python Community.

Much of Fredriks early work can be found in the effBot site on the internet archive


Example code

Pillow/PIL has a very easy and clean interface. With just a few lines of code you could transform images and perform advanced image processing. Here is some examples, what you could do.


Create thumbnails from the command line

In this simple example we use the method - thumbnail, to create thumbnails for images.


# thumb.py
# The script will create thumbnails for file/files given in the script argument.

import sys
from pathlib import Path

from PIL import Image

size = (128, 128)

for infile in sys.argv[1:]:
    outfile: str = Path(infile).stem + ".thumbnail"
    if not outfile != outfile:
        try:
            with Image.open(infile) as im:
                im.thumbnail(size)
                im.save(outfile, "JPEG")
        except OSError:
            print("cannot create thumbnail for", infile)

Usage:

python thumb.py my_images.gif

Display meta data

Here is a example how to display meta data for a image


# meta.py
# This script will display meta data for a image

from PIL import Image

img = Image.open("Streamlit.png")

#Display Image meta data

print(f'Format: {img.format}')
print(f'Filename: {img.filename}')
print(f'Height: {img.height}')
print(f'Width: {img.width}') 
print(f'Size: {img.size}')  

Output:

Format: PNG

Filename: Streamlit.png

Height: 616

Width: 445

Size: (445, 616)

Rotate a image 90 degree

Here is a example to rotate a single image 90 degree


# rotate.py
# This script will rotate a single image 90 degree

from PIL import Image
import PIL

# creating a image object (main image)
im1 = Image.open(r"geek.jpg")

# rotating a image 90 deg counter clockwise
im1 = im1.rotate(90, PIL.Image.NEAREST, expand = 1)

# to show specified image
im1.show()

Sources


Install

pip install Pillow