python Image 旋转图片
时间: 2023-07-25 11:15:23 浏览: 171
可以使用 Python 的 Pillow 库来旋转图片。以下是示例代码:
```python
from PIL import Image
# 读取图片
img = Image.open('image.jpg')
# 设置旋转角度
angle = 45
# 执行旋转操作
rotated = img.rotate(angle)
# 显示旋转后的图片
rotated.show()
```
上述代码中,首先使用 `Image.open()` 函数读取图片,然后使用 `rotate()` 函数执行旋转操作,最后使用 `show()` 函数显示旋转后的图片。注意,旋转角度是以顺时针方向为正方向的。
相关问题
python image 本地图片
### Python 处理本地图片的方法
对于处理本地图片,`Pillow` 是一个非常流行的图像处理库。该库提供了丰富的功能来加载、转换和保存多种图像文件格式。
#### 安装 Pillow 库
为了使用 `Pillow` 来处理图片,首先需要安装这个库:
```bash
pip install pillow
```
#### 加载并显示一张图片
下面是一个简单的例子展示如何打开并显示一张本地存储的图片[^1]:
```python
from PIL import Image
img = Image.open('path_to_image.jpg')
img.show()
```
#### 图片的基本操作
可以执行诸如调整大小、旋转以及翻转等基本变换。例如,要创建一个缩放后的版本:
```python
resized_img = img.resize((new_width, new_height))
resized_img.save('resized_path_to_image.jpg')
```
还可以通过指定角度参数轻松实现图像旋转:
```python
rotated_img = img.rotate(45) # 逆时针旋转45度
rotated_img.save('rotated_path_to_image.jpg')
```
如果想要水平或垂直翻转,则可调用相应的方法:
```python
flipped_h_img = img.transpose(Image.FLIP_LEFT_RIGHT)
flipped_v_img = img.transpose(Image.FLIP_TOP_BOTTOM)
flipped_h_img.save('horizontal_flip_path_to_image.jpg')
flipped_v_img.save('vertical_flip_path_to_image.jpg')
```
#### 提取元数据信息
除了修改图像本身外,有时也需要获取有关图像的信息,比如尺寸或者模式(RGB/CMYK)。这可以通过访问属性完成:
```python
print(f"Image size: {img.size}")
print(f"Image mode: {img.mode}")
```
以上就是利用 `Pillow` 进行一些基础性的本地图片处理的方式[^2]。
python怎么旋转图片
你可以使用Python的Pillow库来旋转图片。下面是一个简单的示例代码:
```python
from PIL import Image
image = Image.open("image.jpg")
# 使用rotate方法旋转图像
rotated_image = image.rotate(45)
# 保存旋转后的图像
rotated_image.save("rotated_image.jpg")
```
在这个例子中,我们打开了一个名为"image.jpg"的图像文件,并使用`rotate()`方法将其旋转了45度。最后,我们保存了旋转后的图像到名为"rotated_image.jpg"的新文件中。
阅读全文
相关推荐














