Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if image is dark? (high contrast, low brightness)

As part of a project I am working on, I need to simply analyze a picture using a CLI Linux application and determining if its dark image (high contrast, low brightness).

So far, I figured out I can use ImageMagick to get verbose information of the image, but not sure how to use that data...or is there a simpler solution?

like image 662
Gregorio Di Stefano Avatar asked Oct 28 '11 23:10

Gregorio Di Stefano


1 Answers

You could scale the image to a very small one -- one that has a dimension of 1x1 pixels and represents the "average color" of your original image:

 convert  original.jpeg  -resize 1x1  1pixel-original.jpeg

Then investigate that single pixel's color, first

convert  1pixel-original.jpeg  1pixel-jpeg.txt 

then

cat 1pixel-jpeg.txt

  # ImageMagick pixel enumeration: 1,1,255,srgb
  0,0: (130,113,108)  #82716C  srgb(130,113,108)

You can also get the same result in one go:

convert  original.jpeg  -resize 1x1  txt:-

  # ImageMagick pixel enumeration: 1,1,255,srgb
  0,0: (130,113,108)  #82716C  srgb(130,113,108)

This way you get the values for your "avarage pixel" in the original color space of your input image, which you can evaluate for its 'brightness' (however you define that).

You could convert your image to grayscale and then resize. This way you'll get the gray value as a measure of 'brightness':

convert  original.jpeg  -colorspace gray  -resize 1x1  txt:-

  # ImageMagick pixel enumeration: 1,1,255,gray
  0,0: (117,117,117)  #757575  gray(117,117,117)

You can also convert your image to HSB space (hue, saturation, brightness) and do the same thing:

convert  original.jpeg  -colorspace hsb  -resize 1x1  txt:-

  # ImageMagick pixel enumeration: 1,1,255,hsb
  0,0: ( 61, 62,134)  #3D3E86  hsb(24.1138%,24.1764%,52.4941%)

The 'brightness' values you see here (either of 134, #86 or 52.4941%) is probably what you want to know.

like image 136
Kurt Pfeifle Avatar answered Oct 28 '22 22:10

Kurt Pfeifle