简体   繁体   English

将PIL numpy 3d数组转换为2d luma值

[英]convert PIL numpy 3d array to 2d luma values

I've loaded an image using: 我已使用以下图片加载了图片:

import numpy as np
from PIL import Image
imag = Image.open("image.png")    
I = np.asarray(imag)

Where the shape of I is (951, 1200, 3) I的形状是(951, 1200, 3)

But I would like to average each pixel roughly to it's luma values ( (r*g*b)/3 ) to make the shape (951, 1200, 1) . 但我想将每个像素大致平均到其亮度值( (r*g*b)/3 )以形成形状(951, 1200, 1)

What is the proper numpy operator to do this? 什么是适当的numpy运算符来做到这一点?

I think the easiest thing is to use Pillow's built-in conversion to Luminance as follows: 我认为最简单的方法是使用Pillow的内置转换为Luminance,如下所示:

import numpy as np
from PIL import Image

# Load image and convert to luminance, and thence to Numpy array
imag = Image.open("image.png").convert('L')    
I = np.asarray(imag)

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

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