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

和自甴很熟 提交于 2019-12-21 02:42:21

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7935814/how-to-determine-if-image-is-dark-high-contrast-low-brightness

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!