Skip to content

Instantly share code, notes, and snippets.

@kmohrf
Created February 25, 2017 13:34
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kmohrf/8d4653536aaa88965a69a06b81bcb022 to your computer and use it in GitHub Desktop.
Save kmohrf/8d4653536aaa88965a69a06b81bcb022 to your computer and use it in GitHub Desktop.
Calculate Image brightness with Python Pillow
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)))
@TylerG398
Copy link

@kmohrf #kmohrf I know this is super old but I'm trying to understand what the 'L' means in your script?

@kmohrf
Copy link
Author

kmohrf commented Jun 25, 2019

@TylerG398 see the reference in the PIL Image.convert documentation or more specifically the list of available modes. It basically converts the image to black and white.

@imneonizer
Copy link

imneonizer commented Jun 26, 2019

Hey i have the exact problem.. i hope this link
https://github.com/imneonizer/How-to-find-if-an-image-is-bright-or-dark/
would help you out achieving the same.
In this blog i have explained how i divided the image into smaller segments and find brightness in those segments and finally took the average of all those calculated brightness to get the brightness level of image
It can easily tell which images are brighter and which are dark.
Hope it could help you with something.

@TylerG398
Copy link

TylerG398 commented Jun 26, 2019

@kmohrf so in this would 1 be white and 0 be black? when I run it on a dark image I get +/- .3 where a light image I get roughly +/- .8

@imneonizer
Copy link

In my case imaged below 45 threshold were considered dark..
Try to play with image sizes and Percentage of circles
In this code is 3% distance between circles try to have 1% for a better precision

@TylerG398
Copy link

In my case imaged below 45 threshold were considered dark..
Try to play with image sizes and Percentage of circles
In this code is 3% distance between circles try to have 1% for a better precision

so what is the number representative of... I understand brightness but what is it specifically? whats it comparing it to? and circles? what do you mean by circles? and if I use PIL and manually set the brightness of an image to .5 that's saying set it to 50% of original brightness, however, I want to match the brightness of another image, will I be able to literally set the second image to the result of this code? or is that the wrong number?

@imneonizer
Copy link

This code gives average brightness level of an image, those circles are indicating that we are picking 3x3 matrix segment of image from that part and finding the brightness and we are doing it for each segment i.e each circles and at the end we are finding average out of them.
There is no absolute value for brightness or darkness higher the value more brighter the image

@sammilei
Copy link

Does anyone know the motivation for the implementation? why does it subtract from the length/range of the histogram?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment