Skip to main content
Participating Frequently
December 28, 2022
Answered

Regarding batch processing for color matching

  • December 28, 2022
  • 3 replies
  • 2095 views

Hello all ,

I want to match colors between two images for a set of 100 pairs . Is it possible in Adobe photoshop to automate the process of matching colors for an entire folder of 100 pair of images .

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

@277251443czo – Try the following script:

 

/*
Match Color File Pairs From 2 Source Folders.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/regarding-batch-processing-for-color-matching/td-p/13449438
Stephen Marsh, v1.0 - 28th December 2022
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/batch-script/td-p/12700356
*/

#target photoshop

    (function () {

        if (app.documents.length === 0) {

            try {

                // Input folder 1
                var folder1 = Folder.selectDialog("Select the source image folder:");
                if (folder1 === null) {
                    alert('Script cancelled!');
                    return;
                }

                // Input folder 2
                var folder2 = Folder.selectDialog("Select the destination image folder:");
                if (folder2 === null) {
                    alert('Script cancelled!');
                    return;
                }

                // Validate input folder selection
                var validateInputDir = (folder1.fsName === folder2.fsName);
                if (validateInputDir === true) {
                    alert("Script cancelled as both the input folders are the same!");
                    return;
                }

                // Limit the file input to png
                var list1 = folder1.getFiles(/\.(png)$/i);
                var list2 = folder2.getFiles(/\.(png)$/i);

                // Alpha-numeric sort
                list1.sort();
                list2.sort();

                // Validate that folder 1 & 2 lists are not empty 
                var validateEmptyList = (list1.length > 0 && list2.length > 0);
                if (validateEmptyList === false) {
                    alert("Script cancelled as one of the input folders is empty or doesn't contain PNG files!");
                    return;
                }

                // Validate that the item count in folder 1 & 2 matches
                var validateListLength = (list1.length === list2.length);
                if (validateListLength === false) {
                    alert("Script cancelled as the input folders don't have equal quantities of images!");
                    return;
                }

                // Output folder
                var saveFolder = Folder.selectDialog("Please select the folder to save to...");

                // Save and set the dialog display settings
                var savedDisplayDialogs = app.displayDialogs;
                app.displayDialogs = DialogModes.NO;

                // PNG save options
                var pngOptions = new PNGSaveOptions();
                pngOptions.compression = 0; // compression value: 0-9
                pngOptions.interlaced = false;

                // Set the file processing counter
                var counter = 0;

                // Perform the processing and saving
                for (var i = 0; i < list1.length; i++) {
                    open(list1[i]);
                    var doc2 = open(list2[i]);
                    var docName = doc2.name.replace(/\.[^\.]+$/, '.png');
                    matchColor(100, 100, 0, true);
                    doc2.saveAs(new File(saveFolder + '/' + docName), pngOptions);
                    counter++;
                    while (app.documents.length > 0) {
                        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                    }
                }

                // End of script
                app.displayDialogs = savedDisplayDialogs;
                app.beep();
                alert('Script completed!' + '\r' + counter + ' output files of ' + list1.length + ' input file pairs saved to:' + '\r' + saveFolder.fsName);

            } catch (err) {
                while (app.documents.length > 0) {
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
                alert(err);
            }

        } else {
            alert('Please close all open documents before running this script!');
        }

        function matchColor(lightness, colorRange, fade, selection) {
            var sourceDoc = app.documents[0].name; // [0] - the first doc open
            function s2t(s) {
                return app.stringIDToTypeID(s);
            }
            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();
            descriptor.putInteger(s2t("lightness"), lightness);
            descriptor.putInteger(s2t("colorRange"), colorRange);
            descriptor.putInteger(s2t("fade"), fade);
            descriptor.putBoolean(s2t("selection"), selection);
            reference.putProperty(s2t("layer"), s2t("background"));
            reference.putName(s2t("document"), sourceDoc); // variable for source doc name
            descriptor.putReference(s2t("source"), reference);
            executeAction(s2t("matchColor"), descriptor, DialogModes.NO);
        }
    }());

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html#Photoshop

3 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
December 28, 2022

@277251443czo – Try the following script:

 

/*
Match Color File Pairs From 2 Source Folders.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/regarding-batch-processing-for-color-matching/td-p/13449438
Stephen Marsh, v1.0 - 28th December 2022
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/batch-script/td-p/12700356
*/

#target photoshop

    (function () {

        if (app.documents.length === 0) {

            try {

                // Input folder 1
                var folder1 = Folder.selectDialog("Select the source image folder:");
                if (folder1 === null) {
                    alert('Script cancelled!');
                    return;
                }

                // Input folder 2
                var folder2 = Folder.selectDialog("Select the destination image folder:");
                if (folder2 === null) {
                    alert('Script cancelled!');
                    return;
                }

                // Validate input folder selection
                var validateInputDir = (folder1.fsName === folder2.fsName);
                if (validateInputDir === true) {
                    alert("Script cancelled as both the input folders are the same!");
                    return;
                }

                // Limit the file input to png
                var list1 = folder1.getFiles(/\.(png)$/i);
                var list2 = folder2.getFiles(/\.(png)$/i);

                // Alpha-numeric sort
                list1.sort();
                list2.sort();

                // Validate that folder 1 & 2 lists are not empty 
                var validateEmptyList = (list1.length > 0 && list2.length > 0);
                if (validateEmptyList === false) {
                    alert("Script cancelled as one of the input folders is empty or doesn't contain PNG files!");
                    return;
                }

                // Validate that the item count in folder 1 & 2 matches
                var validateListLength = (list1.length === list2.length);
                if (validateListLength === false) {
                    alert("Script cancelled as the input folders don't have equal quantities of images!");
                    return;
                }

                // Output folder
                var saveFolder = Folder.selectDialog("Please select the folder to save to...");

                // Save and set the dialog display settings
                var savedDisplayDialogs = app.displayDialogs;
                app.displayDialogs = DialogModes.NO;

                // PNG save options
                var pngOptions = new PNGSaveOptions();
                pngOptions.compression = 0; // compression value: 0-9
                pngOptions.interlaced = false;

                // Set the file processing counter
                var counter = 0;

                // Perform the processing and saving
                for (var i = 0; i < list1.length; i++) {
                    open(list1[i]);
                    var doc2 = open(list2[i]);
                    var docName = doc2.name.replace(/\.[^\.]+$/, '.png');
                    matchColor(100, 100, 0, true);
                    doc2.saveAs(new File(saveFolder + '/' + docName), pngOptions);
                    counter++;
                    while (app.documents.length > 0) {
                        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                    }
                }

                // End of script
                app.displayDialogs = savedDisplayDialogs;
                app.beep();
                alert('Script completed!' + '\r' + counter + ' output files of ' + list1.length + ' input file pairs saved to:' + '\r' + saveFolder.fsName);

            } catch (err) {
                while (app.documents.length > 0) {
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
                alert(err);
            }

        } else {
            alert('Please close all open documents before running this script!');
        }

        function matchColor(lightness, colorRange, fade, selection) {
            var sourceDoc = app.documents[0].name; // [0] - the first doc open
            function s2t(s) {
                return app.stringIDToTypeID(s);
            }
            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();
            descriptor.putInteger(s2t("lightness"), lightness);
            descriptor.putInteger(s2t("colorRange"), colorRange);
            descriptor.putInteger(s2t("fade"), fade);
            descriptor.putBoolean(s2t("selection"), selection);
            reference.putProperty(s2t("layer"), s2t("background"));
            reference.putName(s2t("document"), sourceDoc); // variable for source doc name
            descriptor.putReference(s2t("source"), reference);
            executeAction(s2t("matchColor"), descriptor, DialogModes.NO);
        }
    }());

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html#Photoshop

Participating Frequently
December 28, 2022

Thank you so much Stephen . This helped me a lot .

Stephen Marsh
Community Expert
Community Expert
December 28, 2022

@277251443czo – You’re welcome!

Stephen Marsh
Community Expert
Community Expert
December 28, 2022

A script can batch open pairs of images for processing, but once the two images are open how do you plan to "colour match" them?

Participating Frequently
December 28, 2022

Hi Stephen ,

I have a folder of stereo pair of images , i want to match the colors of right image with the left image .

For an individual pair , I will open both the images (left and right pair) and I will go to the second image and then 

(Image--> Adjustments--> Match color (Will set the source to the left image.)

I want to know if it is possible to automate for a folder of image pairs .

Is it possible with scripting?

Thanking you,

Stephen Marsh
Community Expert
Community Expert
December 28, 2022

@277251443czo 

 

If you have tested that this works to your satisfaction when performed manually, then yes, this can be scripted.

 

How are the file pairs named and is this consistent for alphabetical sorting? Such as:

 

File-A.jpg

File-B.jpg

 

File-C.jpg

File-D.jpg

 

Or perhaps:

 

Doc1A.jpg

Doc1B.jpg

 

Doc2A.jpg

Doc2B.jpg

 

Which file would be the source and which the destination?

 

Do you require a particular layer stack, or just a flattened result?

 

What file format and options should the file be saved to?

 

Do you wish to create a new 3rd file, or overwrite the file that has been processed?

 

What else?

 

Mylenium
Legend
December 28, 2022

Not really. Beyond the various "auto..." functions already there there's no way to match multiple photos consistently without actually opening them and manually dialling in a few tweaks.

 

Mylenium