我该如何获取图像中特定像素的亮度测量值?我需要一种绝对的比较不同像素亮度的刻度。谢谢。
要获取像素的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评论的亮度公式:(确定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))
.convert("L")
。当将一张彩色图片转换为灰度图像(模式“L”)时,库会使用 ITU-R 601-2 亮度变换。 - Константин Ван