简体   繁体   English

Python - 手动将 RGB 图片转换为灰度

[英]Python - Convert RGB picture to Gray Manually

I want to convert an rgb image to 2d matrix in gray.我想将 rgb 图像转换为灰色的二维矩阵。 How can I do this using loops and PIL?如何使用循环和 PIL 做到这一点? I don't want to use a canned function.我不想使用罐装 function。 How can I do that?我怎样才能做到这一点?

I manipulate a lot of images as NumPy arrays like so:我将很多图像操作为 NumPy arrays ,如下所示:

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM