Skip to main content
Inspiring
December 12, 2011
Answered

Determine Light Area vs. Dark Area

  • December 12, 2011
  • 1 reply
  • 3350 views

Here's another unusual one for you.  Is there a way to have Photoshop determine if the majority of a selected area is lighter color or darker.

As an example, some photos have a fairly consistent brightness/darkness across the image, meaning there aren't any areas that are exceptionally dark or bright.  In other cases, a photo might be average brightness over most of the photo with areas of extreme darkness (basically black) so the "average" brightness is brought down quite a bit by that extra dark area even though the majority of the image isn't necessarily that dark.

Is there any way to split these up to get an estimate of just how much of a selected area is extremely dark, how much is mid-toned, and how much is extremely bright?  Thanks!

dgolberg

This topic has been closed for replies.
Correct answer Michael_L_Hale

Could you posterize with 3 levels then read 0,127 & 255 from the histogram?


Following c.pfaffenbichler suggestion you could do something like this to get the pixels below 33 lum ...

var hist = app.activeDocument.histogram;

var count = 0;

var dark = 0;

for( var index = 0; index < 256; index++ ) {

    count = count + hist[ index ];

    if( index < (33*(100/255)) ) dark = dark + hist[ index ];

}

alert("Pixels in selection: " + count + "\rPixels below 33 lum: " + dark +"\rPercent below 33 lum " + ( dark/ count ) * 100 );

You can edit the if index statement to get midtones or highlights or add a couple more if statements to get all three in one pass.

1 reply

dgolbergAuthor
Inspiring
December 12, 2011

You can get a visual representation of what I'm trying to do by going to Filters > Artistic > Cutout, and setting the "Number of Levels" to 3 and the "Edge Simplicity" to 0.  I just need to somehow figure out how much of the picture is made up of the light, how much are the dark, and how much are the mid-tone pixels in this representation and store that info in a variable(s).

Note: The image/selection you use this on should be the same general hue/saturation and only have major variations in brightness to get the desired result.  Alternatively, just de-saturate an image before applying the filter to get a decent representation.

c.pfaffenbichler
Community Expert
Community Expert
December 13, 2011

You could use the Histogram.

Inspiring
December 13, 2011

What is it with HSB?

You could use the composite Histogram, but if you are (edit) determined to use HSB you might be able to use an Lab-copies L-channel’s Histogram – though I’m not sure if that will in fact produce the exact same results.

Anyway, try

alert (app.activeDocument.histogram)

to see what you get.

The Histogram should be limited to the Selection and is an Array, so if you want to know how many pixels are below a certain value you could use a for-function to add all the numbers below the corresponding index-value.

To determine their percentage-value you would have to add up all the values (unless it is a rectangular selection, in which case multiplying the width and height in pixels would be faster) and have the Script do the math.


Could you posterize with 3 levels then read 0,127 & 255 from the histogram?