Using Pillow in Python A Comprehensive Guide

Pillow is a powerful Python library that enables you to perform a wide range of image processing tasks. It’s an open-source fork of the original Python Imaging Library (PIL) and offers a user-friendly interface for handling images in Python. In this article, we’ll explore the capabilities of Pillow and demonstrate its usage with practical examples.
Installation
Section titled “Installation”Before you can use Pillow, you first need to install it. You can do this using pip, the Python package installer.
pip install pillowOpening and Saving Images
Section titled “Opening and Saving Images”To get started with Pillow, you first need to import the Image module from the PIL package.
from PIL import ImageNow, let’s open an image file using the Image.open() method. This method takes a single argument - the path to the image file.
image = Image.open('path/to/image.jpg')Once you’ve opened an image, you can save it in a different format using the save() method.
image.save('path/to/new_image.png')Basic Image Operations
Section titled “Basic Image Operations”Pillow provides several basic image operations, such as resizing, rotating, and flipping.
Resizing
Section titled “Resizing”To resize an image, use the resize() method. This method takes a tuple containing the new width and height.
resized_image = image.resize((new_width, new_height))Rotating
Section titled “Rotating”To rotate an image, use the rotate() method. This method takes a single argument - the angle of rotation in degrees.
rotated_image = image.rotate(45) ## Rotate the image by 45 degreesFlipping
Section titled “Flipping”You can flip an image either horizontally or vertically using the transpose() method.
from PIL import ImageOps
## Flip the image horizontallyflipped_image = ImageOps.flip(image)
## Flip the image verticallymirrored_image = ImageOps.mirror(image)Image Filtering and Enhancement
Section titled “Image Filtering and Enhancement”Pillow also provides various filters and enhancements for images. Let’s explore some of them.
Applying Filters
Section titled “Applying Filters”To apply a filter to an image, use the filter() method. For example, let’s apply a Gaussian blur to an image.
from PIL import ImageFilter
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=5))Adjusting Brightness, Contrast, and Saturation
Section titled “Adjusting Brightness, Contrast, and Saturation”You can adjust the brightness, contrast, and saturation of an image using the ImageEnhance module.
from PIL import ImageEnhance
## Increase brightness by a factor of 1.5enhancer = ImageEnhance.Brightness(image)brightened_image = enhancer.enhance(1.5)
## Increase contrast by a factor of 2enhancer = ImageEnhance.Contrast(image)high_contrast_image = enhancer.enhance(2)
## Decrease saturation by a factor of 0.5enhancer = ImageEnhance.Color(image)desaturated_image = enhancer.enhance(0.5)Working with Image Metadata
Section titled “Working with Image Metadata”Pillow allows you to access and modify the metadata of an image, such as its format, size, and EXIF data.
Format and Size
Section titled “Format and Size”You can obtain the format and size of an image using the format and size attributes.
print(f'Image format: {image.format}')print(f'Image size: {image.size}')EXIF Data
Section titled “EXIF Data”To access the EXIF data of an image, use the _getexif() method.
exif_data = image._getexif()This method returns a dictionary with the EXIF tags as keys and their values as the corresponding values.
Conclusion
Section titled “Conclusion”In this article, we’ve demonstrated how to use Pillow for basic image operations, applying filters and enhancements, and working with image metadata. Pillow is a versatile library that can help you accomplish various image processing tasks in Python. For more information, refer to the official Pillow documentation.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js