Skip to main content
andreamike
Participant
September 23, 2019
Question

Save As PNG script

  • September 23, 2019
  • 4 replies
  • 5037 views

Hello,

 

I'm trying to build a script (I don't know anything about coding in JavaScript) that grabs PSD from a folder and save them to PNG with color profile embedded, in a subfolder called PNG.

Like Image Processor does but for PNG.

I know you can do it with an action but I need more flexibility. I have tryied Image Processor Pro but do not give the option to set the compression.

 

Google can find a thread related to this argument but seams to be archived now.

https://forums.adobe.com/thread/1511528

 

Any help?

 

var inputFolder = Folder.selectDialog("Select a folder to process");
var fileList = inputFolder.getFiles("*.psd");

   pngOptions = new PNGSaveOptions()
   pngOptions.compression = 0
   pngOptions.interlaced = false

   savePath = File(originalDoc.path + "/" + originalDoc.name.replace(/\.[^\.]+$/, '.png'));
    
    newDoc.saveAs(savePath, pngOptions, false, Extension.LOWERCASE)
    
  
    newDoc.close()
    
    app.activeDocument=originalDoc

 

 

This topic has been closed for replies.

4 replies

Stephen Marsh
Community Expert
Community Expert
September 29, 2019

The following script retains the original colour space and embeds the same ICC colour profile into the PNG, while saving to a subdirectory of the input folder named PNG:

 

#target photoshop

// Batch Export SfW PNG to PNG Subfolder.jsx

displayDialogs = DialogModes.NO

// raw.githubusercontent.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx/master/Automated%20Resize%20To%20Web.jsx

if (BridgeTalk.appName == "photoshop") {
    app.bringToFront;

    var inputFolder = Folder.selectDialog("Select the source folder that contains the files for PNG export:");

    if (inputFolder != null) {
        var fileList = inputFolder.getFiles(/\.psd$/i);
        var outputFolder = new Folder(decodeURI(inputFolder) + "/PNG"); // Change the name of the output folder but dont remove the  /forward slash

        if (outputFolder.exists == false) outputFolder.create();

        for (var i = 0; i < fileList.length; i++) {
            if (fileList[i] instanceof File) {
                var document = open (fileList [i]);
                var documentName = fileList [i].name.replace(/\.[^\.]+$/, ''); // Regex remove filename extension

                while (app.documents.length) {
                    var newFile = new File(decodeURI(outputFolder) + "/" + documentName + ".png");

                    // document.flatten (); // Disable flatten image step

 ///// <a href="https://github.com/LeZuse/photoshop-scripts/blob/master/default/Image%20Processor.jsx" target="_blank">https://github.com/LeZuse/photoshop-scripts/blob/master/default/Image%20Processor.jsx</a> ////////////////////////////////
                    /* function ConvertTosRGBProfile() {
                            app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                        } */
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    exportOptions = new ExportOptionsSaveForWeb();
                    exportOptions.format = SaveDocumentType.PNG;
                    exportOptions.PNG8 = false; // false = PNG-24
                    exportOptions.transparency = true; // true = transparent
                    exportOptions.interlaced = false; // true = interlacing on
                    exportOptions.includeProfile = true; // false = don't embedd ICC profile

                    document.exportDocument(newFile, ExportType.SAVEFORWEB, exportOptions);
                    document.close(SaveOptions.DONOTSAVECHANGES);
                }
            }
            if (i == fileList.length - 1) {
                alert("All PSD files have been saved as PNG files!");
            }
        }
    }
}
jamesm79696514
Participant
October 1, 2019
That's works!! Thank you!
andreamike
Participant
September 29, 2019

I was wondering if there is a way to save the PNG into a SubFolder of the InputFolder named 'PNG'

andreamike
Participant
September 29, 2019

So I found this way around. It wokrs but you need to set Photoshop to preserve the embedded color profile.

                    function colorProfileType() {
                        app.activeDocument.colorProfileType = ColorProfile.custom;
                    }

 

Stephen Marsh
Community Expert
Community Expert
September 24, 2019

I just hacked a script that I previously hacked from another script... Hope it helps (there is both a save for web and save as version):

 

https://gist.github.com/MarshySwamp/597818a60c2210f849ccc21c0e5e30b7

https://gist.github.com/MarshySwamp/a5135a6281632ac3316b66546dc39775

 

In the save as version, I found very large file sizes using pngOptions.compression = 0

Any value from 1 - 9 was preferrable

 

#target photoshop

// Batch Export SfW sRGB PNG.jsx

displayDialogs = DialogModes.NO

// raw.githubusercontent.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx/master/Automated%20Resize%20To%20Web.jsx

if (BridgeTalk.appName == "photoshop") {
    app.bringToFront;

    var inputFolder = Folder.selectDialog("Select the source folder that contains the PSD files for PNG export:");

    if (inputFolder != null) {
        var fileList = inputFolder.getFiles(/\.(psd)$/i);
        var outputFolder = inputFolder ;

        for (var i = 0; i < fileList.length; i++) {
            if (fileList[i] instanceof File) {
                var document = open (fileList [i]);
                var documentName = fileList [i].name.replace(/\.[^\.]+$/, ''); // Regex remove filename extension

                while (app.documents.length) {
                    var newFile = new File(decodeURI(outputFolder) + "/" + documentName + ".png");

                    // document.flatten (); // Disable flatten image step

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    function ConvertTosRGBProfile() {
                            app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    exportOptions = new ExportOptionsSaveForWeb();
                    exportOptions.format = SaveDocumentType.PNG;
                    exportOptions.PNG8 = false; // false = PNG-24
                    exportOptions.transparency = true; // true = transparent
                    exportOptions.interlaced = false; // true = interlacing on
                    exportOptions.includeProfile = true; // false = don't embedd ICC profile
                    document.exportDocument(newFile, ExportType.SAVEFORWEB, exportOptions);
                    document.close(SaveOptions.DONOTSAVECHANGES);
                }
            }
            if (i == fileList.length - 1) {
                alert("All PSD files have been saved as PNG files!");
            }
        }
    }
}

 

#target photoshop

// Batch Save As sRGB PNG.jsx

displayDialogs = DialogModes.NO

// raw.githubusercontent.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx/master/Automated%20Resize%20To%20Web.jsx

if (BridgeTalk.appName == "photoshop") {
    app.bringToFront;

    var inputFolder = Folder.selectDialog("Select the source folder that contains the PSD files for save as PNG:");

    if (inputFolder != null) {
        var fileList = inputFolder.getFiles(/\.(psd)$/i);
        var outputFolder = inputFolder;

        for (var i = 0; i < fileList.length; i++) {
            if (fileList[i] instanceof File) {
                var document = open(fileList[i]);
                var documentName = fileList[i].name.replace(/\.[^\.]+$/, ''); // Regex remove filename extension

                while (app.documents.length) {
                    var newFile = new File(decodeURI(outputFolder) + "/" + documentName + ".png");

                    // document.flatten (); // Disable flatten image step

                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    function ConvertTosRGBProfile() {
                        app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                    }
                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    var pngOptions = new PNGSaveOptions();
                    pngOptions.compression = 0
                    pngOptions.interlaced = false;
                    app.activeDocument.saveAs(newFile, pngOptions, true, Extension.LOWERCASE);
                    app.activeDocument.close();

                }
            }
            if (i == fileList.length - 1) {
                alert("All PSD files have been saved as PNG files!");
            }
        }
    }
}

 

andreamike
Participant
September 24, 2019

It works but unfortunately this script converts everything to sRGB IEC61966-2.1. The PSD files I have are carrying different ICC Profile.

And seams that if I add this to the PNG expoert setting do not work as it should.

exportOptions.includeProfile = true

Stephen Marsh
Community Expert
Community Expert
September 24, 2019

The sRGB conversation code block is clearly/obviously identified for easy removal.

 

I would be wary of a PNG file that was not in sRGB space.

 

Save as PNG does not support embedded ICC profiles, while Save for Web does.