Home > Backend Development > Python Tutorial > Quickly get started with the Pillow library: installation tutorial

Quickly get started with the Pillow library: installation tutorial

WBOY
Release: 2024-01-17 09:12:07
Original
1329 people have browsed it

Quickly get started with the Pillow library: installation tutorial

Pillow library is a widely used image processing library. It provides many useful functions and methods for reading, processing, saving and displaying images. In the Python development process, the Pillow library is one of the essential tools. This article will introduce the installation steps and common functions of the Pillow library, and give specific code examples so that you can get started quickly.

1. Install the Pillow library

First, we need to use pip to install the Pillow library. Open a command line window (Windows users) or terminal (Mac users) and enter the following command to install:

pip install Pillow
Copy after login

After the installation is complete, we need to import the Pillow library into the Python code:

from PIL import Image
Copy after login

2. Image reading and saving

Pillow library provides many functions and methods for reading, saving and displaying images. The following is a sample code for reading and saving images:

from PIL import Image

# 读取图像
im = Image.open('path/to/image.jpg')

# 展示图像
im.show()

# 保存图像
im.save('path/to/new/image.jpg')
Copy after login

In the above code, we first read an image using the Image.open() function, and use The im.show() method displays the image. Then, we saved the image using the im.save() method. When saving, you need to specify the save path and saved file name.

3. Image processing

Pillow library provides many useful functions and methods for image processing, such as resizing images, rotating images, cropping images, etc. The following is sample code for some commonly used image processing functions and methods:

  1. Resize the image:
from PIL import Image

# 读取图像
im = Image.open('path/to/image.jpg')

# 调整图像大小
new_size = (800, 800)
im_resized = im.resize(new_size)

# 展示调整后的图像
im_resized.show()

# 保存调整后的图像
im_resized.save('path/to/new/image.jpg')
Copy after login

In the above code, we used im. resize() method to resize the image. This method requires one parameter, the new image size.

  1. Rotate the image:
from PIL import Image

# 读取图像
im = Image.open('path/to/image.jpg')

# 旋转图像
angle = 45
im_rotated = im.rotate(angle)

# 展示旋转后的图像
im_rotated.show()

# 保存旋转后的图像
im_rotated.save('path/to/new/image.jpg')
Copy after login

In the above code, we used the im.rotate() method to rotate the image. This method requires one parameter, the angle of rotation.

  1. Crop the image:
from PIL import Image

# 读取图像
im = Image.open('path/to/image.jpg')

# 裁剪图像
area = (100, 100, 500, 500)
im_cropped = im.crop(area)

# 展示裁剪后的图像
im_cropped.show()

# 保存裁剪后的图像
im_cropped.save('path/to/new/image.jpg')
Copy after login

In the above code, we used the im.crop() method to crop the image. This method requires one parameter, which is the cropped area. This area consists of four numbers, which are the coordinates of the upper left corner and the coordinates of the lower right corner.

4. Image processing summary

Through the above image processing example code, we can see that the Pillow library provides many useful functions and methods for image processing. You can perform image processing according to your own needs to achieve the effect you want.

5. Summary

This article introduces the installation steps and common functions of the Pillow library, and gives specific code examples to help you quickly get started with the Pillow library. The Pillow library is a very powerful image processing library that can help us perform image processing more conveniently during Python development. I hope this article can be helpful to you, thank you for reading!

The above is the detailed content of Quickly get started with the Pillow library: installation tutorial. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template