Pillow Tutorial

Pillow is the main library for image processing in Python.

Pillow is a friendly fork of the Python Imaging Library (PIL).

Since the original PIL is no longer maintained, Pillow has taken over its role and continues active development.

Pillow provides extensive file format support, efficient internal representation, and powerful image processing capabilities.


Who is this tutorial for?

This tutorial is suitable for developers with a basic understanding of Python.

If you don't know Python yet, you can first learn thePython Tutorial。


Main Features

  • Supports multiple image formats (JPEG, PNG, GIF, BMP, TIFF, etc.)
  • Basic image processing operations (rotation, scaling, cropping, etc.)
  • Image filtering and enhancement
  • Supports image drawing
  • Color space conversion

Comparison with Other Libraries

  • Compared to OpenCV: Pillow focuses more on image processing rather than computer vision, and its API is simpler
  • Compared to Matplotlib: Pillow focuses more on image processing rather than data visualization
  • Compared to scikit-image: Pillow is more basic, easier to get started with, and suitable for general image processing tasks

First Pillow Example

After installing Pillow, you still use import PIL orfrom PIL import ...to import the module.

The following example displays basic information about an image:

Example

from PIL import Image

# Open an image
img = Image.open("example.jpeg")

# Display the image
img.show()

# Get image information
print("Image format:", img.format)  # JPEG, PNG, etc.
print("Image size:", img.size)    # (width, height)
print("Image mode:", img.mode)   # RGB, L, etc.

# Save in PNG format
img.save("example.png")

Output:

图片格式: JPEG
图片大小: (987, 987)
图片模式: RGB

After running, the image will also be saved asexample.png。


Related Content

Other Extensions