What it is
Pillow is a modern fork of the Python Imaging Library (PIL) that adds support for opening, manipulating, and saving many different image file formats in Python.
Pillow provides an Image class to handle images, along with functions to resize, crop, rotate, convert formats, apply filters, draw text, and handle transparency. It integrates seamlessly with NumPy arrays for advanced image processing.
Installation
pip install pillowGetting started
The smallest useful thing you can do with it, and what each part means.
from PIL import Image
img = Image.open('image.jpg')
img.show()from PIL import Image
img = Image.open('image.jpg')
resized = img.resize((256, 256))
resized.show()from PIL import Image
img = Image.open('image.png')
img.save('image.jpg')Advanced usage
Where the library earns its place over a simpler alternative.
from PIL import Image
img = Image.open('image.jpg')
cropped = img.crop((50, 50, 200, 200))
cropped.show()from PIL import Image
img = Image.open('image.jpg')
rotated = img.rotate(45)
rotated.show()from PIL import Image, ImageFilter
img = Image.open('image.jpg')
blurred = img.filter(ImageFilter.GaussianBlur(5))
blurred.show()from PIL import Image, ImageDraw, ImageFont
img = Image.open('image.jpg')
draw = ImageDraw.Draw(img)
font = ImageFont.load_default()
draw.text((10,10), 'Hello, Pillow!', fill='white', font=font)
img.show()Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- FileNotFoundError
- Ensure the image file path is correct before opening.
- OSError: cannot identify image file
- Verify that the file is a valid image and Pillow supports its format.
Best practices
- Use `with Image.open(...) as img:` to ensure proper resource handling.
- Convert images to RGB mode before saving in formats that don’t support alpha channels.
- Use thumbnail() for memory-efficient resizing.
- Combine Pillow with NumPy for advanced image processing.
- Handle exceptions for file operations to prevent crashes.
Background
Why it exists, and what it was reacting to.
Pillow was created in 2010 as a friendly fork of PIL to continue development and maintain compatibility with newer versions of Python. It has become the de facto library for image processing in Python, widely used for applications such as image manipulation, format conversion, and automated image processing pipelines.
