如何解决Python - 手动将 RGB 图片转换为灰色
我想将 rgb 图像转换为灰色的二维矩阵。我如何使用循环和 PIL 来做到这一点?我不想使用固定功能。我该怎么做?
解决方法
我将很多图像作为 NumPy 数组进行操作,如下所示:
import numpy as np
from PIL import Image
# Load image
imgIn = Image.open(''c:/path/to/my/input/file.jpg'')
imgArray = np.array(imgIn)
#Do whatever manipulations to the image you need to,e.g.,grayArray = np.mean(imgArray,axis=2)
#Save the final result
imgOut = Image.fromarray(grayArray)
imgOut.save('c:/path/to/my/output/file.jpg')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。