Home  >  Article  >  Backend Development  >  How to crop and rotate images using Python

How to crop and rotate images using Python

王林
王林Original
2023-08-25 20:36:151439browse

How to crop and rotate images using Python

How to use Python to crop and rotate images

Introduction:
In the modern social media era, image processing has become one of the common needs in people's daily lives. one. In order to meet users' needs for image editing, Python provides a powerful image processing library PIL (Python Imaging Library). This article will introduce how to use the PIL library in Python to crop and rotate images, with code examples.

1. Install the PIL library
Before using the PIL library to crop and rotate images, we need to install the PIL library first. Open the command line terminal and execute the following command to install the PIL library:

pip install pillow

2. Image cropping operation
The image cropping operation can be achieved by using the crop() function of the PIL library. crop()The function accepts four parameters, which are the abscissa and ordinate of the upper left corner, and the abscissa and ordinate of the lower right corner. The following is an example that demonstrates how to crop an image:

from PIL import Image

# 打开图片
image = Image.open('input.jpg')

# 图像裁剪
box = (100, 100, 500, 500)
cropped_image = image.crop(box)

# 保存裁剪后的图片
cropped_image.save('output.jpg')

In the above example, we open an image named input.jpg and then use crop( )The function defines a cropping box. The upper left corner of the cropping box is (100, 100) and the lower right corner is (500, 500), that is, a rectangular area in the picture is cropped. Finally, we save the cropped image as a file named output.jpg.

3. Image rotation operation
The image rotation operation can be achieved by using the rotate() function of the PIL library. rotate()The function accepts a parameter indicating the angle of rotation. Here is an example that demonstrates how to rotate an image:

from PIL import Image

# 打开图片
image = Image.open('input.jpg')

# 图像旋转
rotated_image = image.rotate(45)

# 保存旋转后的图片
rotated_image.save('output.jpg')

In the above example, we open an image named input.jpg and then use rotate( ) function rotates the image 45 degrees. Finally, we save the rotated image as a file named output.jpg.

To sum up, this article introduces how to use the PIL library in Python to crop and rotate images, and provides corresponding code examples. By using the PIL library, we can easily perform various editing operations on pictures to meet users' needs for picture editing. I hope that readers can have a deeper understanding and mastery of image processing technology in Python through the introduction of this article.

Reference:

  1. "Pillow Documentation", https://pillow.readthedocs.io/en/stable/, Accessed date: 2021-11-10.

The above is the detailed content of How to crop and rotate images using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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