python 中如何将RGB图像模式转为灰度?

时间:2026-02-12 09:18:43

1、安装pil 扩展,如果没有安装。

pip install pillow

2、安装后,直接引用

from PIL import Image

3、img = Image.open(r'D:/cat.jpg')

4、grey = img.convert('L')

参数L,代表灰度

5、grey.save('D:/grey.jpg')

将转换后的灰度图像对象存储。

python 中如何将RGB图像模式转为灰度?

6、img.show() 原图查看

grey.show()更改为灰色模式后图查

更新模式前后效果比较。

python 中如何将RGB图像模式转为灰度?

© 2026 五度知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com