Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python -- measuring pixel brightness

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

like image 410
Double AA Avatar asked Jun 22 '11 15:06

Double AA


People also ask

How do you find the brightness of an image?

Compute the laplacian of the gray scale of the image. obtain the max value using minMacLoc. call it maxval. Estimate your sharpness/brightness index as - (maxval * average V channel values) / (average of the hist values from 0th channel), as said above.

What is the brightness of a pixel?

For a grayscale images, the pixel value is a single number that represents the brightness of the pixel. The most common pixel format is the byte image, where this number is stored as an 8-bit integer giving a range of possible values from 0 to 255. Typically zero is taken to be black, and 255 is taken to be white.


1 Answers

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 

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)

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))
like image 130
Saúl Pilatowsky-Cameo Avatar answered Oct 05 '22 09:10

Saúl Pilatowsky-Cameo