David's Blog

Using Pillow in Python A Comprehensive Guide

By David Li on 2024-05-24T17:15:41.000Z

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

Before you can use Pillow, you first need to install it. You can do this using pip, the Python package installer.

pip install pillow

Opening and Saving Images

To get started with Pillow, you first need to import the Image module from the PIL package.

from PIL import Image

Now, 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

Pillow provides several basic image operations, such as resizing, rotating, and flipping.

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

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 degrees

Flipping

You can flip an image either horizontally or vertically using the transpose() method.

from PIL import ImageOps

## Flip the image horizontally
flipped_image = ImageOps.flip(image)

## Flip the image vertically
mirrored_image = ImageOps.mirror(image)

Image Filtering and Enhancement

Pillow also provides various filters and enhancements for images. Let’s explore some of them.

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

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.5
enhancer = ImageEnhance.Brightness(image)
brightened_image = enhancer.enhance(1.5)

## Increase contrast by a factor of 2
enhancer = ImageEnhance.Contrast(image)
high_contrast_image = enhancer.enhance(2)

## Decrease saturation by a factor of 0.5
enhancer = ImageEnhance.Color(image)
desaturated_image = enhancer.enhance(0.5)

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

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

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

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.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-11-25.