python中怎么用rotate函数旋转图片45度
时间: 2023-08-12 08:03:54 浏览: 103
你可以使用Pillow库中的`Image`模块来旋转图片,具体步骤如下:
1.安装Pillow库
如果你还没有安装Pillow库,可以使用以下命令进行安装:
```
pip install Pillow
```
2.导入模块
在代码中导入Pillow库中的`Image`模块:
```python
from PIL import Image
```
3.打开图片
使用`Image.open()`函数打开需要旋转的图片,例如:
```python
image = Image.open('example.jpg')
```
4.旋转图片
使用`Image.rotate()`函数对图片进行旋转,例如:
```python
new_image = image.rotate(45)
```
这将会把原图旋转45度,返回一个新的旋转后的图片对象`new_image`。
5.保存图片
最后,使用`Image.save()`函数将旋转后的图片保存到指定的路径,例如:
```python
new_image.save('example_rotated.jpg')
```
完整代码示例:
```python
from PIL import Image
# 打开图片
image = Image.open('example.jpg')
# 旋转图片
new_image = image.rotate(45)
# 保存图片
new_image.save('example_rotated.jpg')
```
相关问题
python将图片旋转45度
可以使用Pillow库来实现图片旋转功能。以下是示例代码:
```python
from PIL import Image
# 打开图片
image = Image.open('example.jpg')
# 旋转45度
rotated_image = image.rotate(45)
# 保存旋转后的图片
rotated_image.save('rotated_example.jpg')
```
在代码中,我们首先打开需要旋转的图片,然后使用`rotate()`函数将图片旋转45度,最后使用`save()`函数保存旋转后的图片。请替换`example.jpg`为你需要旋转的图片路径。
Python中如何实现图片旋转?
Python中,可以使用PIL库中的Image模块来实现图片旋转。具体步骤如下:
```python
from PIL import Image
# 打开图片
img = Image.open('image.jpg')
# 旋转图片
img_rotate = img.rotate(45) # 旋转45度
# 显示图片
img_rotate.show()
# 保存图片
img_rotate.save('image_rotate.jpg')
```
另外,如果需要对图片进行裁剪,可以使用Pillow库中的Image模块的crop()函数。具体步骤如下:
```python
from PIL import Image
# 打开图片
img = Image.open('image.jpg')
# 裁剪图片
box = (100, 100, 300, 300) # 左上角和右下角坐标
img_crop = img.crop(box)
# 显示图片
img_crop.show()
# 保存图片
img_crop.save('image_crop.jpg')
```