Skip to content

Pillow (PIL) - The Python Imaging Library Notes

Table of Contents (ToC)

Introduction

Pillow (PIL) is a Python library used for opening, manipulating, and saving various image file formats.

What's Pillow (PIL)?

  • A fork of the original Python Imaging Library (PIL).
  • Provides extensive file format support.
  • Simplifies image processing tasks in Python.

Key Concepts and Terminology

  • Image Object: Core object representing an image.
  • Modes: Represent image types (e.g., RGB, RGBA, L, etc.).
  • Filters: Operations that can be applied to images (e.g., BLUR, CONTOUR).

Applications

  • Web development (e.g., image manipulation for web applications).
  • Data analysis (e.g., processing images for machine learning).
  • Graphics design (e.g., automating repetitive image editing tasks).

Fundamentals

Pillow (PIL) Architecture Pipeline

  • Image Loading
  • Open images using Image.open().
  • Supported formats: JPEG, PNG, BMP, etc.
  • Image Processing
  • Apply transformations (resize, crop, rotate).
  • Enhance images (adjust color, contrast, brightness).
  • Image Saving
  • Save images using Image.save().
  • Export formats: JPEG, PNG, GIF, etc.

How Pillow (PIL) works?

  • Import the library: from PIL import Image.
  • Load an image: image = Image.open('example.jpg').
  • Perform operations: image = image.resize((100, 100)).
  • Save the image: image.save('example_resized.jpg').

Some hands-on examples

  • Opening an image: Image.open('example.jpg').
  • Converting image mode: image.convert('L').
  • Applying filters: image.filter(ImageFilter.BLUR).
  • Cropping an image: image.crop((10, 10, 100, 100)).

Tools & Frameworks

  • Pillow: Core library for image processing.
  • Jupyter Notebook: For interactive coding and testing.
  • NumPy: Often used alongside for numerical operations on images.
  • Matplotlib: For visualizing images and results.

Hello World!

from PIL import Image

# Open an image file
image = Image.open('example.jpg')

# Resize the image
image = image.resize((200, 200))

# Save the image
image.save('example_resized.jpg')

# Display the image
image.show()

Lab: Zero to Hero Projects

References