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.

dgolbergAuthor
Inspiring
December 13, 2011

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.


Thanks guys!  That code works really well Michael; and it was easy to modify/adjust!

To answer c.pfaffenbichler's question; 75-90% of what I do with photoshop, currently, uses Saturation and Brightness adjustments; so for me, using the HSB model works best.  I was trained in at work using HSB, RGB, and occasionally CMYK, but with the majority of what I do requiring Saturation or Brightness adjustments (I'm sure there are other ways, but these are what we use now, and they work), I tend to favor HSB (though really it's just SB that I favor, while I generally use the Color Balance tool to change the actual color of things, manually). 

LAB I've never really worked with before (nor been taught how to work with it in Photoshop), so I tend to lean towards the tried and true until someone shows me a good reason as to why I should change methods (unless of course I have extra time to mess around with other methods myself; which I haven't had as of late).

I tried your alert suggestion and got a wall of numbers that, I'm assuming, are the peaks/valleys of the histogram read-outs; though I haven't a clue how to read them/break them up (I'm also assuming that's what Michaels script then did?).  I'm not much of a programmer or Photoshop expert, so anything new, I tend to struggle to figure out at first.  But, I am good at analyzing things and optimizing the process of getting from point A to point B in a project (using logic and the tools I know), so that's what I do for the company I work for.

Thanks again for the help everyone.  I'll be messing around with the code some to see how I can best fit it to what I'm doing; but it appears to be what I'm looking for from what I've been able to test so far.  If I run into any trouble, you'll likely see another post regarding that issue later on .

dgolberg