【问题标题】:Python - Convert RGB picture to Gray ManuallyPython - 手动将 RGB 图片转换为灰度
【发布时间】:2021-04-02 00:44:13
【问题描述】:

我想将 rgb 图像转换为灰色的二维矩阵。如何使用循环和 PIL 做到这一点?我不想使用固定功能。我该怎么做?

【问题讨论】:

标签: python python-3.x image-processing python-imaging-library grayscale


【解决方案1】:

我将很多图像操作为 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')

【讨论】:

    猜你喜欢
    • 2016-10-18
    • 2011-12-03
    • 2015-07-11
    • 1970-01-01
    • 2014-02-26
    • 2014-12-22
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多