python - 测量像素亮度

时间:2011-06-22 15:07:27

标签: python image pixel brightness

如何测量图像中特定像素的像素亮度?我正在寻找一个绝对比例来比较不同像素的亮度。感谢

1 个答案:

答案 0 :(得分:21)

要获取像素的RGB值,您可以使用PIL

from PIL import Image
from math import sqrt
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 

然后,亮度只是从黑色到白色的比例,如果平均三个RGB值,可以提取女孩:

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

或者你可以更深入地使用Ignacio Vazquez-Abrams评论的亮度公式:(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))
相关问题