Skip to main content
Participating Frequently
July 13, 2021
Answered

least blurred image

  • July 13, 2021
  • 4 replies
  • 1794 views

Hi all, I have Adobe Photoshop 21.0.2 and a problem.

I have a set of images (of the same thing, let's say a page with handwritten text), and these images can have a certain degree of blurriness. I wonder if there's a way for Photoshop to tell me which image is the least blurred.

This topic has been closed for replies.
Correct answer Stephen Marsh

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));
}

 

 

4 replies

Stephen Marsh
Community Expert
Community Expert
July 25, 2021

@maurot43944773 – so, how did the script work for you?

Participating Frequently
July 26, 2021

Thank you so much for the script. I didn't know such a thing was possible.

For now I solved my problem by a simple java application that reads all the images in a folder and tell me which one is the sharpest. I though it was faster to load and process than opening PS, load/process all the images, extracting a value from each one, and select the greatest.

Kukurykus
Legend
July 26, 2021

Could do you please do two things:

- share an image you use to test above script with

- mark the given answer as the correct solution 😉

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
July 18, 2021

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));
}

 

 

c.pfaffenbichler
Community Expert
Community Expert
July 14, 2021

Have you read »Depth of field blending« here? 

Combine images with Auto-Blend Layers

Participating Frequently
July 14, 2021

Hi! thank you for your answer. What should I look for in this document?

c.pfaffenbichler
Community Expert
Community Expert
July 15, 2021

»Depth of field blending«? 

Stephen Marsh
Community Expert
Community Expert
July 13, 2021

Two options that spring to mind...

 

* Compare histogram values at 100% view for the image with the higher Std. Dev. value which will be "sharper" (ensure that the histogram is refreshed, uncached)

 

* Use Adobe Bridge and your eyes with the loupe tool (no, I'm not being sarcastic)

 

A third option would be to use shake reduction to examine the PSF "blur estimation region" preview manually for the same selected region for each image.

Participating Frequently
July 14, 2021
Thank you for your answer.
In this way, however, I should make the choice of the better image, and I,
being human, am fallacious. It would be way better if it was a task for PS
to do by itself.
Can this be a feature I could ask?
Participant
February 25, 2022

You can make a feature request here:

 

Submit product feedback

 

For now, I can envision a script where you would select a folder of input images and a text file would be output listing the source filename and the standard deviation value for each file.

 


Greetings, Im new at scripting... Will this method move the most OOF (Out of Focus) Image to a separate folder or vice versa?