如何利用PIL和numpy实现对比度滤镜和饱和度滤镜
时间: 2024-05-16 19:18:05 浏览: 15
要实现对比度滤镜和饱和度滤镜,可以使用PIL库和numpy库。
对比度滤镜:
1. 导入库
```python
from PIL import Image
import numpy as np
```
2. 打开图像
```python
img = Image.open('image.jpg')
```
3. 将图像转换为numpy数组
```python
img_array = np.array(img)
```
4. 计算均值和标准差
```python
mean = np.mean(img_array)
std = np.std(img_array)
```
5. 对图像进行对比度调整
```python
img_con = np.uint8(np.clip((img_array - mean) * (10 / std) + mean, 0, 255))
```
6. 将numpy数组转换为图像
```python
img_con = Image.fromarray(img_con)
img_con.save('image_con.jpg')
```
饱和度滤镜:
1. 导入库
```python
from PIL import Image, ImageEnhance
import numpy as np
```
2. 打开图像
```python
img = Image.open('image.jpg')
```
3. 将图像转换为numpy数组
```python
img_array = np.array(img)
```
4. 将RGB颜色空间转换为HSV颜色空间
```python
hsv_img = Image.fromarray(img_array).convert('HSV')
hsv_img_array = np.array(hsv_img)
```
5. 计算饱和度
```python
saturation = ImageEnhance.Color(hsv_img).enhance(2)
saturation_array = np.array(saturation)
```
6. 将HSV颜色空间转换回RGB颜色空间
```python
rgb_img = Image.fromarray(saturation_array, 'HSV').convert('RGB')
rgb_img.save('image_saturation.jpg')
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)