0

I currently have a code for measuring the average brightness of an RGB image. When i run this with a black and white image, the RGB values are the same, e.g. 37, 37, 37 I have no idea about colours etc but i dont think this is correct

Here is my code at the moment:

from PIL import Image
from math import sqrt

imag = Image.open("../Images/pexels-photo-57905.jpeg")

imag = imag.convert ('RGB')
imag.show()

X,Y = 0,0

pixelRGB = imag.getpixel((X,Y))
R,G,B = pixelRGB
brightness = sum([R,G,B])/3 ##0 is dark (black) and 255 is bright (white)
print(brightness)
print(R,G,B)

In a nutshell, i must convert an RGB image into grayscale, which im using .convert('LA') for, i must THEN measure the brightness of the image by adding the black and white values then dividing them by 2

Acryaz
  • 29
  • 2
  • 5
  • 2
    So what is your question? Are you asking if gray values have the same values for the color components? If this is the question - yes, this is basically the definition of gray values. – MrBean Bremen May 06 '20 at 15:34
  • Yeah basically, if i get a result of 37, 37, 37, for RGB values, would those values be the same in black and white values. My final image must be grayscale converted from RGB and the grayscale brightness must be measured – Acryaz May 06 '20 at 15:37
  • Have a look at https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color for relevant discussion on brightness (luminance, perceived brightness, etc.) – bgstech May 06 '20 at 15:40
  • When the three RGB coefficients are equal, grey is obtained. (0,0,0) corresponds to white, while (255, 255, 255) corresponds to black. Naively brightness is thus the mean of the three RGB coefficients. – nonin May 06 '20 at 15:41
  • Ok, so you are asking if RGB(37, 37, 37) is the same as 37 in monochrome image. Basically yes. If you convert non-grayscale to grayscale you have to make some correction for the perceived different luminosity of different color components, but in the case of grayscale RGB to grayscale just this doesn't matter. Does [this](https://stackoverflow.com/questions/687261/converting-rgb-to-grayscale-intensity) answer your question? – MrBean Bremen May 06 '20 at 15:42

1 Answers1

-1

Are these codes correct to measure the average brightness of a greyscale image? What do the three lines below mean? Does it return the average brightness of the whole picture, or just (0,0)?

X,Y = 0,0
pixelRGB = imag.getpixel((X,Y))
R,G,B = pixelRGB
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 06 '22 at 06:37
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31467754) – Enrique Benito Casado Apr 08 '22 at 14:25