Skip to main content
staydown.outlaw
Participating Frequently
February 4, 2022
Question

how do I export layers as .png without loosing the size and placement of the particular layer?

  • February 4, 2022
  • 3 replies
  • 6830 views

Please, this is driving me crazy. I am exporting my layers individually and I lose the size and where this layer was placed. In my 1st Photo see how the 3 hearts are covering basically the whole canvas? 2nd pic there are no hearts but all that color are hair, shirt, eyes, back ground... I am using export as, now I go to canvas and input the proper px width, height, I also preserve details, then export it and now as seen in the 3rd pic I have the size that I originally wanted but the placement is still in the middle of the canvas. I dont understand what im doing wrong. Ive got a project with 18 groups each consisting of no less than 15 layers. Ive already gone thru 3 seperate times exporting individually thinking Ive figured it out but I havent and that is time consuming and definately tries my patience. what should I do?

This topic has been closed for replies.

3 replies

Stephen Marsh
Community Expert
Community Expert
February 7, 2022

Similar to the previous script, this version uses Save for Web to export to PNG every top-level layer or layer group as a separate image. I have not added checks or graceful handling of duplicate layer names, the expectation is that every layer will produce a unique filename.

 

/*
All top level layers exported to PNG.jsx
Stephen Marsh, 5th February 2022 - v1.0
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
Based on:
https://damienfremont.com/2019/05/25/how-to-photoshop-script-export-layers-to-png-files/
Note: Existing files will be overwritten!
*/

#target photoshop

if (app.documents.length > 0) {

    try {
        var outputPath = app.activeDocument.path.fsName;
    } catch (e) {
        var outputPath = Folder.selectDialog("Unsaved base file, select the output folder:");
    }
    
    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 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 + "/" + filename + "_" + layerName + ".png"), ExportType.SAVEFORWEB, pngOptions);
    }
    alert("PNG files saved to:" + "\r" + outputPath);

} else {
    alert("You must have a document open!");
}

 

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

staydown.outlaw
Participating Frequently
February 8, 2022

dont want you to think i am ignoring you or the time you spend trying to help me. I appreciate it very much. unfortuneatly I have some family issues I must deal with before I can put my head into this. I will get to this and thank you very much for your time and help. I will let you know how it goes when I am able to. thanks again

Kukurykus
Legend
March 26, 2022

Since six and the half week passed can you make a time to try shared solution?

Stephen Marsh
Community Expert
Community Expert
February 7, 2022

This scipt will save as to PNG every top-level layer or layer group as a separate image. I have not added checks or graceful handling of duplicate layer names, the expectation is that every layer will produce a unique filename.

 

/*
All top level layers saved to PNG.jsx
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
Stephen Marsh, 5th February 2022 - v1.0
Based on:
https://damienfremont.com/2019/05/25/how-to-photoshop-script-export-layers-to-png-files/
Note: Existing files will be overwritten!
*/

#target photoshop

if (app.documents.length > 0) {

    try {
        var outputPath = app.activeDocument.path.fsName;
    } catch (e) {
        var outputPath = Folder.selectDialog("Unsaved base file, select the output folder:");
    }

    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 file = new File(outputPath + "/" + filename + "_" + layerName + ".png");
        
        var saveOptions = new PNGSaveOptions();
        saveOptions.compression = 0; // 0-9
        saveOptions.interlaced = false;
        app.activeDocument.saveAs(file, saveOptions, true, Extension.LOWERCASE);
    }
    alert("PNG files saved to:" + "\r" + outputPath);

} else {
    alert("You must have a document open!");
}

 

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

Participant
August 11, 2023

Hi, if I want to save the layers in a specific folder and not the folder of the orginal files, which lines of code should I replace with the path of the folder?

Stephen Marsh
Community Expert
Community Expert
August 12, 2023
quote

Hi, if I want to save the layers in a specific folder and not the folder of the orginal files, which lines of code should I replace with the path of the folder?


By @Dã Hạc30787016q755

 

If you wish to use a dialog to select, change this try/catch block from:

 

    try {
        var outputPath = app.activeDocument.path.fsName;
    } catch (e) {
        var outputPath = Folder.selectDialog("Unsaved base file, select the output folder:");
    }

 

To:

 

var outputPath = Folder.selectDialog("Select the output folder:");

 

Or if you prefer to use a common, cross-platform location such as the user's Desktop, or Documents folders:

 

var outputPath = "~/Desktop";

 

Otherwise put in the full path to the folder/directory...

 

Mac example:

 

var outputPath = "/Users/user name/the folder"

 

Windows example (directory path using cross-platform JS single forward slashes): 

 

var outputPath = "C:/the parent folder/the child directory";

 

Windows example (directory path using Windows back slashes, but they need to be JS escaped/double): 

 

var outputPath = "C:\\the parent folder\\the child directory";

 

Have fun!

Legend
February 4, 2022

Maybe you need to add registration marks to your design.

staydown.outlaw
Participating Frequently
February 4, 2022

what is that?

 

Leslie Moak Murray
Community Expert
Community Expert
February 4, 2022

They are marks placed outside the cropped/ active area to allow the printer to align the layers (or letterpress plates, as the case may be). It can be those target icons or any other mark, and they get cropped/trimmed off. For my work (greeting cards), it's crop marks that are used.