2

I need to determine if an image is above a certain brightness. Using a scale of 0 - 255 I want to generate a value within this range to reflect the image brightness.

i.e. a white image is 255 and a black image is 0.

All this needs to take place via a bash script I am building up at the moment. I have no idea what image lib could do this for me though.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
dibs
  • 1,018
  • 2
  • 23
  • 35

2 Answers2

6

Generally, it's one of the classic problems of signal processing and there are several approaches, based on how do you define "brightness". It's generally the same for "brightness" of an image, "loudness" of a sound signal, etc.

Some ideas of what you can use as a generic "brightness" is:

  • Average value of all the pixels (i.e. sum up all the brightnesses of all the pixels, then divide by total amount of pixels, i.e. width * height).
  • Build up a histogram of brightness distribution, then choose such point x in that histogram that 98% (95%, 90%, 70% - this number can vary) of all the pixels in your image would be less bright than this x. It's called percentile.
  • Calculate "root mean square" (RMS) for all the pixels (sum up squares of all the pixels, divide by total amount of pixels, extract square root from this one).

There are multiple image libraries that can yield good results. The easiest one to use from a shell script is probably ImageMagick/GraphicsMagick - you can get both simple averages and do some more complex histogramming to check out percentiles if you want to.

GreyCat
  • 16,622
  • 18
  • 74
  • 112
5

Try ImageMagick gray stats or histogram

convert rose: -colorspace gray -format "%[fx:100*mean]%%" info:
Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
  • 1
    I am looking at re-sampling the image to a 1x1 px monochrome image to get the average value I need to compare it against other images so this is probably the closest suggestion to what I think is the right answer. – dibs Nov 23 '10 at 04:40