2011-06-22 50 views
11

如何获得图像中特定像素的像素亮度测量值?我正在寻找绝对比例来比较不同像素的亮度。由于python - 测量像素亮度

+2

[公式来确定RGB颜色的亮度](http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color) –

+2

重复是假设它只是你需要帮助的部分 - 在这种情况下,“python”标签完全不相关,因为你不关心代码,仅仅是规模。如果你真的关心Python方面,需要更多的信息(PIL,PyQt4,Something Else?) –

+0

我建议你从标题和标签中删除python,因为这不是编程语言特定的 – Vitor

回答

17

要获得像素的RGB值,你可以使用PIL

import Image 
imag = Image.open("yourimage.yourextension") 
#Convert the image te RGB if it is a .gif for example 
imag = imag.convert ('RGB') 
#coordinates of the pixel 
X,Y = 0,0 
#Get RGB 
pixelRGB = imag.getpixel((X,Y)) 
R,G,B = pixelRGB 

然后,亮度是简单地从黑色到白色的规模,女巫可以,如果你平均3个RGB值提取:

brightness = sum([R,G,B])/3 ##0 is dark (black) and 255 is bright (white) 

或者你可以去更深,使用亮度公式伊格纳西奥巴斯克斯 - 艾布拉姆斯评论有关:(Formula to determine brightness of RGB color

#Standard 
LuminanceA = (0.2126*R) + (0.7152*G) + (0.0722*B) 
#Percieved A 
LuminanceB = (0.299*R + 0.587*G + 0.114*B) 
#Perceived B, slower to calculate 
LuminanceC = sqrt(0.299*R^2 + 0.587*G^2 + 0.114*B^2) 
+0

作品完美无缺,是我的扫描有一个“洗”黑色的大帮助... – Tim

+0

不应该pixelRGB =图像.getpixel((X,Y)) R,G,B = pixelRGB – RobotHumans

+0

如何获得alpha分量? –