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

前端 未结 1 1384
有刺的猬
有刺的猬 2021-02-09 17:45

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).

相关标签:
1条回答
  • 2021-02-09 18:11

    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.

    0 讨论(0)
提交回复
热议问题