Copy link to clipboard
Copied
Hi Community,
Is it possible to check median values of the current active Layer image via Photoshop scripting ?
I am planning to write a script to check if the current layer's median value is greater than median >= 250.
Similar to this one - How do you get Histogram Mean Value? But instead of Mean I would like to get median values.
Please help me in this ?
Thanks
Copy link to clipboard
Copied
I think this might work to give you the median value. It basically get the image's pixel dimension, then cuts that in half, as the median is the middle value. The it just goes through the histogram and adds up the values in each value (how many pixels have a value of 0 etc). When it reaches the middle point of pixels, it stops and shows the array number of the histogram that contains that value.
#target photoshop
var doc = activeDocument;
var dimTotal = parseInt(doc.width*doc.height);
var dimHalf = dimTotal/2
var histo = doc.histogram
var runningCount = 0
for(var i=0;i<histo.length;i++){
runningCount +=histo;
if(runningCount>=dimHalf){
alert('i= '+ i)
break;
}
}
Copy link to clipboard
Copied
I can’t write scripts so I am not sure if this will help or not… I pulled this from another script, but it appears to work correctly simply swapping mean to median…though I am not sure about the activeLayer vs. activeDocument:
var mean = meanHist(activeDocument.histogram).toFixed(0);
var median = medianHist(activeDocument.histogram).toFixed(0);