Lukkar
Lukkar

Reputation: 111

Getting width and height of image without need of opening it in PS - CS6 script

I strugled to search the answer in documentation (1, 2) but I couldn't find anyone.

Consider that we have array of files from certain folder located in hard drive. We want to filter these files in this array depending on dimensions of its files.
That means, filter images which both sides (width or height) are higher than some value and lower than some value at the same time.

Of course we can open each file in PS as Document and check each one by property app.activeDocument.width or app.activeDocument.height and choose highest one and match it. But it is extremely inefficient, especially when we have hundreds of files but only few of them meet conditions.

So my question is:
Is there any possibility to check width and height without opening the image?

If this is possible to do without a need of installing 3rd party software, it's great.
If not, it would be great that you don't have to install it manually (some stand alone version).
Even when condition above are not met, it's better any solution than no one.

code:

var sourceFolder = Folder.selectDialog("Select folder with files to process");

var LowestValueOfSides = 50; // Any arbitrary value

var HighestValueOfSides = 100; // Any arbitrary value

var FilesToProcess = getFilesToProcess(sourceFolder, LowestValueOfSides, HighestValueOfSides);

alert(FilesToProcess);

function getFilesToProcess(sourceFolder, LowestValueOfSides, HighestValueOfSides) {

    sourceFilesUnfiltered = sourceFolder.getFiles();
    var properFilesExtPSfiles = /.(jpg|tif|psd|bmp|gif|png)$/;

    var sourceFilesFilteredPSD = filteringSourceFilesByExtensions(sourceFilesUnfiltered, properFilesExtPSfiles);

    var sourceFilesFilteredByRes = filteringSourceFilesByRes(sourceFilesFilteredPSD, LowestValueOfSides, HighestValueOfSides);

    return sourceFilesFilteredByRes;
}

function filteringSourceFilesByExtensions(sourceFilesUnfiltered, properFilesExtPSfiles) {

    var sourceFilesFiltered = new Array;

    for (var i = 0; i < sourceFilesUnfiltered.length; i++) { 
        if (sourceFilesUnfiltered[i] instanceof File) {

            var sourceFilePathString = sourceFilesUnfiltered[i].toString();

            var sourceFileToMatch = decodeURIComponent(sourceFilePathString);

            if (sourceFileToMatch.match(properFilesExtPSfiles)) {// decodeURIComponent(), to avoid problem when you have special signs in source files

                sourceFilesFiltered.push( File(sourceFilePathString) );
            } 
        } 
    }

    return sourceFilesFiltered;
}

function filteringSourceFilesByRes(sourceFilesFilteredPSD, LowestValueOfSides, HighestValueOfSides) {

    var sourceFilesFilteredByRes = new Array;

    for (var i = 0; i < sourceFilesFilteredPSD.length; i++) {

        var longestSide = getLongestSideValue(sourceFilesFilteredPSD[i]); //   <====== this is function which I have problem

        if ( (longestSide >= LowestValueOfSides) && (longestSide <= HighestValueOfSides)) {
            sourceFilesFilteredByRes.push(sourceFilesFilteredPSD[i]);
        }
    }

    return sourceFilesFilteredByRes;
}

function getLongestSideValue(File) { //   <====== this is function which I have problem

    var FileWidth // <===== I need this value
    var FileHeight // <===== I need this value

    var longestSideValue = Math.max(FileWidth, FileHeight);

    return longestSideValue;
}

Thanks in advance.

Upvotes: 1

Views: 458

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208003

Not sure what sort of answer you are looking for because you seem to want to write Javascript but you don't want Photoshop to open your images, so I wanted to advise that you can use ImageMagick to get the width and height of all your JPEG/PNG images easily enough in Terminal (Command Prompt) like this:

magick identify -format "%hx%w - %f\n" *.jpg *.png
1936x2592 - iphone.jpg
100x100 - lime.jpg
600x600 - out.jpg
400x400 - paddington.jpg
100x100 - red.jpg
1936x2592 - result-fft.jpg
50x50 - result.jpg
178x178 - 1.png
178x178 - 2.png

Note that PSD and TIFF files often have multiple layers, so you may want to add the layer number in there for those types:

magick identify -format "%hx%w - %f[%s]\n" *psd *tif
768x1024 - a.psd[0]
768x1024 - a.psd[1]
248x1280 - a.tif[0]

Upvotes: 2

Related Questions