SoFunction
Updated on 2025-03-01

Python's image processing library Pillow installation and use tutorial

Preface

In Python, the Pillow library is a very powerful image processing library. It provides a wide range of image processing functions, allowing us to easily operate images and realize image conversion, cropping, scaling, rotation and other operations. In addition, Pillow also supports reading and saving of various image formats, including JPEG, PNG, BMP, GIF, etc.

Install Pillow

First, we need to install the Pillow library. Enter the following command in the terminal or command line to install Pillow:

pip install pillow 

If your environment already has PIL library installed, you can upgrade to the latest version of Pillow with the following command:

pip install pillow --upgrade

The main modules and classes of the Pillow library

The main modules of the Pillow library include Image, ImageColor, ImageDraw, ImageFont, ImageFilter, etc. Each module provides corresponding classes and methods to process images.

  • The Image module provides methods to open, operate and save images. It contains all the required image manipulation functions.
  • The ImageColor module provides a method for manipulating RGB color space.
  • The ImageDraw module provides methods for drawing various shapes and text on an image.
  • The ImageFont module provides methods for setting fonts and font sizes.
  • The ImageFilter module provides some methods for filtering images.

Below we will use code examples to demonstrate how to use the Pillow library for image processing.

Open and display images

First, we can use()Method to open an image and use()Method to display images.

from PIL import Image  
  
# Open the imageimg = ('')  
  
# Show image()

In this example, we use()The method opens the nameand save the image file toimgin variable. Then, we use()Method to display this image.

Image conversion and adjustment

The Pillow library provides a variety of methods for converting and adjusting images. Here are some commonly used methods:

  • (size)Methods are used to resize images.
  • (angle, expand=True)Methods are used to rotate images.
  • (method)Methods are used to flip or rotate images.
  • (mode='RGB')Method is used to convert images to RGB mode.
  • (brightness=0, contrast=0, saturation=0, hue=0)Methods are used to adjust the brightness, contrast, saturation and hue of an image.
from PIL import Image, ImageOps, ImageFilter, ImageEnhance  
  
# Open the imageimg = ('')  
  
# Resize imageimg_resized = ((300, 300))  
  
# Rotate the imageimg_rotated = (45)  
  
# Flip the imageimg_flipped = (Image.FLIP_LEFT_RIGHT)  
  
# Adjust brightness contrast and saturationenhancer = (img)  
img_brightened = (1.5)  # Increase brightness by 50%enhancer = (img)  
img_contrasted = (1.5)  # Increase contrast by 50%enhancer = (img)  
img_colored = (1.5)  # Increase saturation by 50%  
# Show resultsimg_resized.show()  
img_rotated.show()  
img_flipped.show()  
img_brightened.show()  
img_contrasted.show()  
img_colored.show()

In this example, we first use()The method opens the nameand save the image file toimgin variable. Then, we use()Method to resize the image, use()Method to rotate the image, use()Method to flip or rotate the image, useImageEnhanceModularBrightness()Contrast()andColor()Methods adjust the brightness, contrast and saturation of the image respectively. Finally, we useimg_xxx.show()Method to display each processed image.

Supplement: What other advanced image processing features does the Pillow library provide?

Filter effect: You can use the ImageFilter module in the Pillow library to apply various filter effects. For example, you can use a Blur to make the image look softer, and an Enhance filter to enhance the edges of the image.

Sample code:

from PIL import Image, ImageFilter

# Read image filesimage = ("")

# Apply a blur filterblurred_image = ()

# Apply enhanced edge filtersedge_enhanced_image = (ImageFilter.EDGE_ENHANCE)

# Display the image after the filter effectblurred_image.show()
edge_enhanced_image.show()

Image Merge: Use()The function can mix two images. This function requires passing two images and a blending coefficient as parameters.

Sample code:

from PIL import Image

# Read the first image fileimage1 = ("")

# Read the second image fileimage2 = ("")

# Set the mixing coefficient to 0.5, that is, the mixing ratio of the two images is 50%.blended_image = (image1, image2, 0.5)

# Show mixed imagesblended_image.show()

Color adjustment: Use the ImageEnhance module in the Pillow library to adjust the color of the image. For example, you can use the Brightness function to increase or decrease the brightness of an image, and use the Contrast function to increase or decrease the contrast of an image.

Sample code:

from PIL import ImageEnhance

# Read image filesimage = ("")

# Create a brightness adjusterbrightness_enhancer = (image)

# Increase image brightnessbrightened_image = brightness_enhancer.enhance(1.5)  # 1.5 is the multiple of the increase
# Create a contrast adjustercontrast_enhancer = (image)

# Increase image contrastcontrasted_image = contrast_enhancer.enhance(1.2)  # 1.2 is the multiple of the increase
# Show the adjusted imagebrightened_image.show()
contrasted_image.show()

These features can help you achieve filter effects, image merging, and color adjustment in image processing. Select the corresponding function and adjust the parameters as needed.

Summarize

This is the article about the installation and use of the Python image processing library Pillow. For more related content of the Python image processing library Pillow, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!