python 旋转图像的函数
时间: 2023-11-22 08:05:43 浏览: 33
可以使用Pillow库中的Image类来旋转图像。以下是一个基本的旋转函数示例:
```python
from PIL import Image
def rotate_image(image_path, degrees_to_rotate, saved_location):
"""
Rotate the given image file by the given number of degrees and save the rotated image to the given path.
"""
# Open the image file
image_obj = Image.open(image_path)
# Rotate the image
rotated_image = image_obj.rotate(degrees_to_rotate)
# Save the rotated image to the desired location
rotated_image.save(saved_location)
print(f"Image rotated by {degrees_to_rotate} degrees and saved to {saved_location}")
```
这个函数需要三个参数:要旋转的图像文件的路径,旋转角度和保存旋转后图像的路径。例如,如果您想将名为“example.jpg”的图像顺时针旋转90度并将其保存为“example_rotated.jpg”,则可以调用此函数:
```python
rotate_image("example.jpg", 90, "example_rotated.jpg")
```
请注意,此函数将图像旋转指定的角度,但不会调整图像的大小或剪裁。如果旋转后的图像尺寸超出原始图像的大小,则可能需要进行其他处理。