Created
February 25, 2017 13:34
-
-
Save kmohrf/8d4653536aaa88965a69a06b81bcb022 to your computer and use it in GitHub Desktop.
Calculate Image brightness with Python Pillow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PIL import Image | |
def calculate_brightness(image): | |
greyscale_image = image.convert('L') | |
histogram = greyscale_image.histogram() | |
pixels = sum(histogram) | |
brightness = scale = len(histogram) | |
for index in range(0, scale): | |
ratio = histogram[index] / pixels | |
brightness += ratio * (-scale + index) | |
return 1 if brightness == 255 else brightness / scale | |
if __name__ == '__main__': | |
for file in sys.argv[1:]: | |
image = Image.open(file) | |
print("%s\t%s" % (file, calculate_brightness(image))) |
Hi, what about measure brightness over selfies?. What happen if person has black or caucasican race?. The measure of face brightness must be independent of person race..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does anyone know the motivation for the implementation? why does it subtract from the length/range of the histogram?