2

Every time I look up a question that displays example code on here or Git I usually see the letter L listed in the explanation. For example the code below for finding the average brightness of an image. (I'm trying to find average brightness of a set of images in a user-specified directory and this is where I was starting.)

I have tried to research it however every time I look it up it comes up with seemingly irrelevant explanations. I want to state that the code I'm showing is NOT MINE and I will link the original user below

import sys
from PIL import Image


def calculate_brightness(image):
    greyscale_image = image.convert('L')  # THIS IS THE '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)))

The link for the original user and their code is: https://gist.github.com/kmohrf/8d4653536aaa88965a69a06b81bcb022

Tyler G
  • 35
  • 7

1 Answers1

1

From the docs for PIL:

The default method of converting a greyscale (“L”)

PyPingu
  • 1,697
  • 1
  • 8
  • 21
  • I did have another quick question, what does the "-" when talking about scale? its (-scale + index) and i cant find any information on the "-" sign for the len(histogram) @PyPingu – Tyler G Jun 25 '19 at 14:28
  • Are you serious? That's a [minus sign](https://docs.python.org/3/library/operator.html#mapping-operators-to-functions) for making the `scale` number negative. `scale` has been set to the length of the histogram (`len(histogram)`) which is an `int` – PyPingu Jun 25 '19 at 14:39
  • yes, im a student learning python – Tyler G Jun 25 '19 at 16:54
  • That’s ok, but if you don’t yet have a grasp of something like `-` I would recommend taking a step back from anything like `PIL` and working through some proper beginner tutorials to help get more acquainted with the basic functionality of the standard libraries – PyPingu Jun 25 '19 at 17:05