SoFunction
Updated on 2024-11-19

A hands-on guide to image processing with Python using Pillow

I. Introduction

Pillow, a branch of the Python Imaging Library (PIL), is used for image processing. It is the most commonly used image-processing library in the Python programming language, providing a wide range of image-processing features, including displaying, manipulating, and saving images.The goal of the Pillow library is to make image processing easier to do in Python.

II. Installation and basic use

First, we need to install the Pillow library in our Python environment. Just type the following command in the command line:

pip install pillow

Once the installation is complete, we can start using Pillow to work with images. First, we need to import the Image module and then open an image file using the () method. This method will return an Image object, which we can manipulate, such as displaying the image.

from PIL import Image

# Open the image
img = ('')

# Display image
()

III. Image processing

The Pillow library provides us with a rich set of image processing features. We will describe some of them in detail below.

1. Crop the image

We can crop an image using the () method. This method takes a four-element tuple parameter representing the left, top, right, and bottom coordinates of the cropping region.

# Trimming
box = (100, 100, 400, 400)
region = (box)
()

2. Rotate the image

We can rotate the image using the () method. This method takes an angle parameter.

# Rotate
img_rotated = (45)
img_rotated.show()

3. Resize the image

We can resize the image using the () method. This method takes a two-element tuple parameter representing the new width and height.

# Resize
img_resized = ((128, 128))
img_resized.show()

4. Change the color

We can change the color of the image using the () method. This method takes a color mode string parameter.

# Change the color
img_gray = ('L')
img_gray.show()

IV. Image preservation and format conversion

After we have processed an image, we may need to save the processed image.The Image object of the Pillow library provides the save() method. This method takes a file path parameter that indicates the path and file name to save the image.

# Save images
('new_example.jpg')

In addition, Pillow library supports conversion of many image formats, including JPEG, PNG, BMP, GIF, PPM and TIFF. We can specify the format of an image directly by the file extension when saving the image. For example, if we want to save an image in PNG format, we just need to set the filename to a name ending in .png.

# Save as PNG
('new_example.png')

Pillow automatically determines the format of the image based on the file extension. If you don't specify a file extension, Pillow will save it in JPEG format by default.

Also, if we need to set some specific parameters when saving the image, such as the JPEG quality, we can pass these parameters as keyword arguments to the save() method. Below is an example:

# Save JPEG images with specified quality
('new_example.jpg', quality=95)

Note that different image formats support different parameters. Some parameters may only work for specific image formats. You can check Pillow's official documentation for more information on saving images and format conversions.

V. Color space and color conversion

Color space conversion is a common task in image processing.The Pillow library supports a variety of color spaces, such as RGB, HSV, and so on. We can use the () method to convert an image from one color space to another.

# Convert the color space
img_hsv = ('HSV')
img_hsv.show()

VI. Image synthesis and stitching

If you have multiple images and want to merge them into a new image, the Pillow library can help you with that as well. We can use the () method to blend two images of the same size, or the () method to synthesize multiple images according to certain rules.

Alternatively, if you want to stitch multiple images into a new image, you can use the () method to create a new blank image and then use the () method to paste other images onto the blank image.

# Create a new blank image
new_img = ('RGB', (500, 500))

# Paste the other image onto the new image
new_img.paste(img, (0, 0))
new_img.paste(img_resized, (200, 200))
new_img.show()

VII. Image mapping and text addition

Pillow also provides some simple graphic drawing functions, such as drawing lines, circles, polygons and so on. We can use the ImageDraw module to do the drawing. Here is a simple example:

from PIL import ImageDraw

draw = (img)
((0, 0) + , fill=128)
((0, [1], [0], 0), fill=128)
()

In addition, Pillow allows us to add text to the image. We can use the () method to add text. Note that adding text requires specifying a font, which requires the use of the ImageFont module.

from PIL import ImageDraw, ImageFont

# Create a font object
font = ('', 15)

draw = (img)
((10, 10), 'Hello, Pillow!', font=font, fill='white')
()

VIII. Conclusion

The above is a brief exploration of Python's Pillow library. In fact, the Pillow library has many other powerful features waiting to be discovered. For example, we can use the Pillow library to work with animated GIFs, or to read and modify the metadata of an image, and so on. Overall, if you need to do image processing in Python, the Pillow library is a good choice.

to this article on the use of Python Pillow image processing practice guide to this article, more related Python Pillow content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!