2011-06-22 48 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分量? –