python - 测量像素亮度

Dou*_* AA 15 python image pixel brightness

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

Sau*_*ila 22

要获得像素的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 
Run Code Online (Sandbox Code Playgroud)

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

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

或者你可以更深入地使用Ignacio Vazquez-Abrams评论的亮度公式:( 确定RGB颜色亮度的公式)

#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))
Run Code Online (Sandbox Code Playgroud)

  • **或者只是 `.convert("L")`。** _将彩色“图像”转换为灰度(模式“L”)时,库使用 ITU-R 601-2 亮度变换。_ (2认同)