python怎么编辑图片
时间: 2024-04-15 08:29:36 浏览: 10
在Python中,可以使用PIL(Python Imaging Library)库来编辑图片。以下是基本的图片编辑操作示例:
1. 安装PIL库:
```shell
pip install pillow
```
2. 导入PIL库:
```python
from PIL import Image
```
3. 打开图片:
```python
image = Image.open("image.jpg")
```
4. 调整图片大小:
```python
new_size = (800, 600)
resized_image = image.resize(new_size)
```
5. 旋转图片:
```python
rotated_image = image.rotate(45)
```
6. 裁剪图片:
```python
box = (100, 100, 500, 400) # 左上角和右下角坐标
cropped_image = image.crop(box)
```
7. 调整图片亮度:
```python
enhancer = ImageEnhance.Brightness(image)
brightened_image = enhancer.enhance(1.5) # 参数大于1增加亮度,小于1降低亮度
```
8. 保存图片:
```python
resized_image.save("resized_image.jpg")
```
这些只是一些基本的图片编辑操作示例,PIL库还提供了更多功能,如调整对比度、添加滤镜等。你可以根据具体需求进一步探索PIL库的功能。