python-测量像素亮度 [英] python -- measuring pixel brightness

查看:554
本文介绍了python-测量像素亮度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何测量图像中特定像素的像素亮度?我正在寻找用于比较不同像素亮度的绝对比例.谢谢

How can I get a measure for a pixels brightness for a specific pixel in an image? I'm looking for an absolute scale for comparing different pixels' brightness. Thanks

推荐答案

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

To get the pixel's RGB value you can use 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值取平均值,则可以提取巫婆:

Then, brightness is simply a scale from black to white, witch can be extracted if you average the three RGB values:

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

或者您可以更深入地使用Ignacio Vazquez-Abrams评论过的亮度公式:(

OR you can go deeper and use the Luminance formula that Ignacio Vazquez-Abrams commented about: ( 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))

这篇关于python-测量像素亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆