在Python中使用PIL(Python Imaging Library)库,您需要先安装Pillow包,因为PIL在Python 3中已经被Pillow替代。以下是安装和使用Pillow的步骤:
安装Pillow
在命令行中输入以下命令来安装Pillow库:
```
pip install pillow
或者
```
pip3 install pillow
导入Pillow库
在Python代码中导入Pillow库:
```python
from PIL import Image
打开图像
使用`Image.open()`函数打开图像文件:
```python
im = Image.open('image.jpg')
显示图像
使用`show()`函数显示图像:
```python
im.show()
保存图像
使用`save()`函数保存图像,需要指定保存路径和文件格式:
```python
im.save('new_image.jpg')
图像处理示例
缩放图像:
```python
new_size = (width, height)
im_resized = im.resize(new_size)
裁剪图像:
```python
box = (left, upper, right, lower)
im_cropped = im.crop(box)
```python
im_gray = im.convert('L')
旋转图像:
```python
im.rotate(45)
应用滤镜:
```python
from PIL import ImageFilter
im_filtered = im.filter(ImageFilter.BLUR)
保存处理后的图像:
```python
im_filtered.save('beautyBlur.jpg')
以上是使用Pillow进行基本图像处理的一些示例。Pillow库提供了丰富的图像处理功能,您可以根据需要选择使用不同的操作。