Pillow: Python image processing library (installation and usage tutorial)

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 manipulate images and implement operations such as image conversion, cropping, scaling, and rotation. In addition, Pillow also supports the 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 a terminal or command line to install Pillow:

pip install pillow 
If the PIL library is already installed in your environment, you can use the following command to upgrade to the latest version of Pillow:
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 for opening, manipulating, and saving images. It contains all needed image manipulation functions.
  • The ImageColor module provides methods for manipulating the RGB color space.
  • The ImageDraw module provides methods for drawing various shapes and text on images.
  • The ImageFont module provides methods for setting the font and font size.
  • The ImageFilter module provides methods for filtering images.

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

Open and display images

First, we can use Image.open()the method to open an image and Image.show()the method to display the image.

from PIL import Image  
  
# 打开图像  
img = Image.open('example.jpg')  
  
# 显示图像  
img.show()

In this example, we use Image.open()the method to open example.jpgthe image file named and save it to imga variable. Then, we use img.show()the method to display this image.

Image conversion and adjustment

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

  • Image.resize(size)method to resize the image.
  • Image.rotate(angle, expand=True)method is used to rotate the image.
  • Image.transpose(method)method to flip or rotate the image.
  • Image.convert(mode='RGB')method is used to convert the image to RGB mode.
  • Image.adjust(brightness=0, contrast=0, saturation=0, hue=0)method for adjusting the brightness, contrast, saturation, and hue of an image.
from PIL import Image, ImageOps, ImageFilter, ImageEnhance  
  
# 打开图像  
img = Image.open('example.jpg')  
  
# 调整图像大小  
img_resized = img.resize((300, 300))  
  
# 旋转图像  
img_rotated = img.rotate(45)  
  
# 翻转图像  
img_flipped = img.transpose(Image.FLIP_LEFT_RIGHT)  
  
# 调整亮度对比度和饱和度  
enhancer = ImageEnhance.Brightness(img)  
img_brightened = enhancer.enhance(1.5)  # 提高亮度50%  
enhancer = ImageEnhance.Contrast(img)  
img_contrasted = enhancer.enhance(1.5)  # 提高对比度50%  
enhancer = ImageEnhance.Color(img)  
img_colored = enhancer.enhance(1.5)  # 提高饱和度50%  
  
# 显示结果  
img_resized.show()  
img_rotated.show()  
img_flipped.show()  
img_brightened.show()  
img_contrasted.show()  
img_colored.show()

In this example, we first Image.open()open example.jpgthe image file named using the method and save it to imga variable. We then Image.resize()resize the image using the method , Image.rotate()rotate the image using the method , flip or rotate the image using the method , and adjust the brightness, contrast, and saturation of the image Image.transpose()using ImageEnhancethe module's Brightness(), Contrast()and methods, respectively. Color()Finally, we use img_xxx.show()method to display each processed image.

Guess you like

Origin blog.csdn.net/qq_72290695/article/details/132571808