The following script will process a top level directory and write out a .csv file to the desktop with the standard deviation value for each image. The highest Std. Dev. value will be the "sharpest" image.
/*
least blurred image
https://community.adobe.com/t5/photoshop/least-blurred-image/m-p/12183817
Recursive processing to CSV code based on:
https://forums.adobe.com/message/9697558#9697558
Standard deviation histogram processing code based on:
https://community.adobe.com/t5/photoshop/how-to-get-the-histogram-s-std-dev/td-p/9875041
*/
#target photoshop;
app.bringToFront();
main();
function main() {
var inputFolder = Folder.selectDialog("Select the folder to process:");
if (inputFolder === null) return;
// Add or remove file extensions as required
var fileList = inputFolder.getFiles(/\.(jpg|dng|tif|psd|crw|cr2|psb|exr|nef|dcr|dc2|erf|raf|orf|tga|mrw|mos|srf|pic|pct|pxr|pdd|pef|png|x3f|rw2)$/i);
var outputFile = File("~/Desktop/" + decodeURI(inputFolder.name) + ".csv");
outputFile.open('w');
outputFile.writeln('Filename,Highest Std. Dev. Value = "Sharpest" Image');
for (var a in fileList) {
open(fileList[a]);
///////////////////////////////////
/*
How to get the histogram's Std Dev
https://community.adobe.com/t5/photoshop/how-to-get-the-histogram-s-std-dev/td-p/9875041
Credit to c.pfaffenbichler for the 2018 code and thanks to Kukurykus for the 2021 update!
*/
if (app.documents.length > 0 && app.activeDocument.mode == DocumentMode.RGB) {
var theR = histogramMeanStandardDeviation(app.activeDocument.channels[0].histogram);
var theG = histogramMeanStandardDeviation(app.activeDocument.channels[1].histogram);
var theB = histogramMeanStandardDeviation(app.activeDocument.channels[2].histogram);
var theStdDev = (((theR[2] + theG[2] + theB[2]) / 3).toFixed(2));
// Write the filename and values
outputFile.writeln(decodeURI(activeDocument.name) + "," + theStdDev);
}
////// get mean of histogram //////
function histogramMeanStandardDeviation(theHist) {
// get total number;
var thePixels = 0;
for (var m = 0; m < theHist.length; m++) {
thePixels = thePixels + theHist[m];
}
// get mean and median;
var theMean = 0;
var aTotal = 0;
var check = false;
for (var n = 0; n < theHist.length; n++) {
theMean = theMean + (n * theHist[n] / thePixels);
aTotal = aTotal + theHist[n];
if (aTotal >= thePixels / 2 && check === false) {
theMedian = n;
check = true;
}
}
// get standard deviation;
var theStandDev = 0;
for (var o = 0; o < theHist.length; o++) {
theStandDev = theStandDev + (Math.pow((o - theMean), 2) * theHist[o]);
}
theStandDev = Math.sqrt(theStandDev / thePixels);
//
return ([theMean, theMedian, theStandDev]);
}
///////////////////////////////////
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
outputFile.close();
app.beep();
alert("CSV created:" + "\n" + decodeURI(outputFile));
}