python图像灰度化处理
时间: 2023-08-27 17:22:44 浏览: 123
要将彩色图像转换为灰度图像,可以使用以下方法之一:
方法1:使用PIL库
```python
from PIL import Image
def convert_to_grayscale(image_path):
image = Image.open(image_path).convert("L")
image.save("grayscale_image.png")
# 调用函数并传入图像路径
convert_to_grayscale("color_image.png")
```
方法2:使用OpenCV库
```python
import cv2
def convert_to_grayscale(image_path):
image = cv2.imread(image_path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite("grayscale_image.png", gray_image)
# 调用函数并传入图像路径
convert_to_grayscale("color_image
阅读全文