Skip to main content
Participating Frequently
May 12, 2023
Answered

Export layers to files at 200% and maintain canvas size

  • May 12, 2023
  • 2 replies
  • 1720 views

Hey everyone,

 

I was about to hire someone to develop a script but i thought i would ask the community first.

 

The issue i am having is that i regularly need to batch export layers to png's. Up to this point, i have been exporting using scripts > export layers to files. The issue i now have is that i need them exported at 200% size which that script doesn't do.

 

I tried using the 'export as' function which does 200% but it clips the layers to the object. I need the export to have the full canvas size and maintain its position. 

 

Does anyone have any ideas?

 

Cheers.

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

@PixelTonic 

 

You can try the following script:

 

/*
All top level layers exported to PNG x2 Size.jsx
Stephen Marsh, 12th May 2023 - v1.1
https://community.adobe.com/t5/photoshop-ecosystem-discussions/export-layers-to-files-at-200-and-maintain-canvas-size/td-p/13787373
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-do-i-export-layers-as-png-without-loosing-the-size-and-placement-of-the-particular-layer/m-p/12734226
Note:
* Save before running the script if desired, as the doc will be closed without saving! 
*/

#target photoshop

    (function () {

        if (app.documents.length > 0) {
            imageSize(200, 200, 0);
            try {
                var outputPath = app.activeDocument.path.fsName;
            } catch (e) {
                var outputPath = Folder.selectDialog("Unsaved base file, select the output folder:");
            }
            var counter = 0;
            for (var i = 0; i < app.activeDocument.layers.length; i++) {
                for (var j = 0; j < app.activeDocument.layers.length; j++) {
                    app.activeDocument.layers[j].visible = false;
                }
                var layerIndex = i;
                app.activeDocument.layers[layerIndex].visible = true;
                var layerName = app.activeDocument.layers[layerIndex].name.replace(/[^a-z0-9 _-]/gi, '').replace(/ +|-+|_+$/g, '');
                var filename = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                var theFile = new File(outputPath + "/" + layerName + ".png");
                // Prompt to overwrite existing file
                if (theFile.exists) {
                    // true = 'No' as default active button
                    if (!confirm("File exists, overwrite: Yes or No?", true))
                        throw null;
                    //throw alert("Script cancelled!");
                }
                var pngOptions = new ExportOptionsSaveForWeb();
                pngOptions.PNG8 = false;
                pngOptions.transparency = true;
                pngOptions.interlaced = false;
                pngOptions.quality = 100;
                pngOptions.includeProfile = true;
                pngOptions.format = SaveDocumentType.PNG;
                app.activeDocument.exportDocument(File(outputPath + "/" + layerName + ".png"), ExportType.SAVEFORWEB, pngOptions);
                counter++;
            }
            activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            alert(counter + " PNG files exported to:" + "\r" + outputPath);
        } else {
            alert("You must have a document open!");
        }

    })();

function imageSize(width, height, noise) {
    function s2t(s) {
        return app.stringIDToTypeID(s);
    }
    var descriptor = new ActionDescriptor();
    descriptor.putUnitDouble(s2t("width"), s2t("percentUnit"), width);
    descriptor.putUnitDouble(s2t("height"), s2t("percentUnit"), height);
    descriptor.putEnumerated(s2t("interfaceIconFrameDimmed"), s2t("interpolationType"), s2t("deepUpscale"));
    descriptor.putInteger(s2t("noise"), noise);
    executeAction(s2t("imageSize"), descriptor, DialogModes.NO);
}

 

NOTE: Save before running the script if desired, as the doc will be closed without saving! 

 

2 replies

Stephen Marsh
Community Expert
Community Expert
May 12, 2023

You can also look at the following scripts with a GUI from Paul Riggott:

 

https://github.com/Paul-Riggott/PS-Scripts/blob/master/Layer%20Saver.jsx

 

https://github.com/Paul-Riggott/PS-Scripts/blob/master/Layer%20Saver%20Plus.jsx

 

You would probably wish to change the line:

 

var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);

 

To:

 

var myBrush = g.newBrush(g.BrushType.THEME_COLOR, "appDialogBackground");

 

 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
May 12, 2023

@PixelTonic 

 

You can try the following script:

 

/*
All top level layers exported to PNG x2 Size.jsx
Stephen Marsh, 12th May 2023 - v1.1
https://community.adobe.com/t5/photoshop-ecosystem-discussions/export-layers-to-files-at-200-and-maintain-canvas-size/td-p/13787373
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-do-i-export-layers-as-png-without-loosing-the-size-and-placement-of-the-particular-layer/m-p/12734226
Note:
* Save before running the script if desired, as the doc will be closed without saving! 
*/

#target photoshop

    (function () {

        if (app.documents.length > 0) {
            imageSize(200, 200, 0);
            try {
                var outputPath = app.activeDocument.path.fsName;
            } catch (e) {
                var outputPath = Folder.selectDialog("Unsaved base file, select the output folder:");
            }
            var counter = 0;
            for (var i = 0; i < app.activeDocument.layers.length; i++) {
                for (var j = 0; j < app.activeDocument.layers.length; j++) {
                    app.activeDocument.layers[j].visible = false;
                }
                var layerIndex = i;
                app.activeDocument.layers[layerIndex].visible = true;
                var layerName = app.activeDocument.layers[layerIndex].name.replace(/[^a-z0-9 _-]/gi, '').replace(/ +|-+|_+$/g, '');
                var filename = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                var theFile = new File(outputPath + "/" + layerName + ".png");
                // Prompt to overwrite existing file
                if (theFile.exists) {
                    // true = 'No' as default active button
                    if (!confirm("File exists, overwrite: Yes or No?", true))
                        throw null;
                    //throw alert("Script cancelled!");
                }
                var pngOptions = new ExportOptionsSaveForWeb();
                pngOptions.PNG8 = false;
                pngOptions.transparency = true;
                pngOptions.interlaced = false;
                pngOptions.quality = 100;
                pngOptions.includeProfile = true;
                pngOptions.format = SaveDocumentType.PNG;
                app.activeDocument.exportDocument(File(outputPath + "/" + layerName + ".png"), ExportType.SAVEFORWEB, pngOptions);
                counter++;
            }
            activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            alert(counter + " PNG files exported to:" + "\r" + outputPath);
        } else {
            alert("You must have a document open!");
        }

    })();

function imageSize(width, height, noise) {
    function s2t(s) {
        return app.stringIDToTypeID(s);
    }
    var descriptor = new ActionDescriptor();
    descriptor.putUnitDouble(s2t("width"), s2t("percentUnit"), width);
    descriptor.putUnitDouble(s2t("height"), s2t("percentUnit"), height);
    descriptor.putEnumerated(s2t("interfaceIconFrameDimmed"), s2t("interpolationType"), s2t("deepUpscale"));
    descriptor.putInteger(s2t("noise"), noise);
    executeAction(s2t("imageSize"), descriptor, DialogModes.NO);
}

 

NOTE: Save before running the script if desired, as the doc will be closed without saving! 

 

Participating Frequently
May 12, 2023

Thanks for that but that still exports it at the same canvas size... 

 

It also gave me some errors while exectuting

Stephen Marsh
Community Expert
Community Expert
May 12, 2023

What Photoshop version are you using? The script is resampling using the Preserve Details 2.0 method.