图像翻转python
时间: 2024-11-22 10:27:37 浏览: 22
在Python中,对图像进行翻转通常可以使用PIL(Pillow)库,它是一个非常流行的图像处理库。要实现图像水平或垂直翻转,你可以按照以下步骤操作:
1. 首先,安装PIL库(如果尚未安装):
```
pip install Pillow
```
2. 然后,导入必要的模块并打开图像文件:
```python
from PIL import Image
image = Image.open('image.jpg')
```
3. 对于水平翻转(左右翻转),使用`transpose()`函数和`Image.FLIP_LEFT_RIGHT`常量:
```python
flipped_image_horizontal = image.transpose(Image.FLIP_LEFT_RIGHT)
flipped_image_horizontal.save('flipped_image_horizontal.jpg')
```
4. 对于垂直翻转(上下翻转),同样使用`transpose()`函数,这次用`Image.FLIP_TOP_BOTTOM`:
```python
flipped_image_vertical = image.transpose(Image.FLIP_TOP_BOTTOM)
flipped_image_vertical.save('flipped_image_vertical.jpg')
```
5. 关闭图像文件:
```python
image.close()
```
阅读全文
相关推荐


















