Skip to main content
staydown.outlaw
Participating Frequently
Feb 4, 2022 3:05 PM UTC
Question

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

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 is closed to new replies. Start a new post to keep the conversation going.

22 replies

Stephen Marsh
Community Expert
Community Expert
Feb 7, 2022 11:49 AM UTC

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

Translate
staydown.outlaw
Participating Frequently
Feb 8, 2022 9:02 AM UTC

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

Translate
Kukurykus
Legend
Mar 26, 2022 10:00 AM UTC

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

Translate
Stephen Marsh
Community Expert
Community Expert
Feb 7, 2022 11:47 AM UTC

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

Translate
Participant
Aug 11, 2023 2:54 PM UTC

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?

Translate
Stephen Marsh
Community Expert
Community Expert
Aug 12, 2023 12:04 AM UTC
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!

Translate
Legend
Feb 4, 2022 3:25 PM UTC

Maybe you need to add registration marks to your design.

Translate
staydown.outlaw
Participating Frequently
Feb 4, 2022 3:55 PM UTC

what is that?

 

Translate
staydown.outlaw
Participating Frequently
Feb 7, 2022 11:10 AM UTC

There should be workarounds via scripts until Adobe get on top of this.


im not quite sure what scripts are but I truly believed i had figured it out and have been all super secret squierrel about it until i was able to re-export and re-name and re-everything basically for the hundreth time but it doesnt matter. i suck. im close but its off and i can not put this out as art or anything really respectable under my name. but maybe... nevermind. booooooooooooooo

Translate