9

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?

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Gregorio Di Stefano
  • 1,173
  • 1
  • 15
  • 23
  • 5
    What constitutes "dark"? You need to define that. For example, it may mean that you average all pixels and categorize the image as "dark" if the result is below a given threshold. Once you have your definition, you can work with the pixel data and start coming up with some code. – JYelton Oct 28 '11 at 23:39

1 Answers1

24

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.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Is this working with the perceived luminosity? I need to composite an image in screen mode to several images, but some of them needs to be darker to the result looks fine. I thought taking the luminosity of the "good images" I know and then make darker those which have a smaller luminosity. But I am getting similar results with good and bad images, any help? – hosseio Dec 02 '15 at 12:56
  • This is a great idea; I was looking at scripting up a wallpaper auto changer that would take the time of day into consideration; dark wallpapers for night time and bright ones for day. @y2josei you can make it more clever by applying statistical distribution analysis of some kind over a larger thumbnail rather than the 1x1; more to script up though. – ϹοδεMεδιϲ Mar 12 '18 at 23:46