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

As part of the project I'm working on, I just need to analyze the image using the Linux CLI application and determine if it has a dark image (high contrast, low brightness).

So far, I realized that I can use ImageMagick to get detailed information about the image, but I'm not sure how to use this data ... or is there a simpler solution?

+4
source share
1 answer

You can scale the image to a very small one, which is 1x1 pixels in size and represents the "medium color" of your original image:

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

Then examine the color of one pixel, 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 at a time:

 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 “pixel with resolution” in the original color space of your input image, which you can evaluate by its “brightness” (however, you determine this).

You can convert the image to shades of gray and then resize it. Thus, you get the value of gray 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 into HSB space (hue, saturation, brightness) and do the same:

 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 ​​that you see here (or 134 , #86 or 52.4941% ) are probably what you want to know.

+15
source

All Articles