psd.js icon indicating copy to clipboard operation
psd.js copied to clipboard

Save the image layer group

Open Allyson-Santana opened this issue 4 years ago • 5 comments

I have a PSD with layer groups. Is it possible to save each layer group as an image?

For Example: Layer group 01: { layer logo layer text to be continued.... } Layer group 02: { layer logo layer text to be continued.... }

I need to download each layer group as a separate image.

Allyson-Santana avatar Jan 05 '22 13:01 Allyson-Santana

Have you found a solution?

Dmarcotrigiano avatar Jan 30 '22 22:01 Dmarcotrigiano

I couldn't find a solution. I'm using the separate layers. Joining the single layers until they form a group of complete layers.

Allyson-Santana avatar Jan 31 '22 13:01 Allyson-Santana

me to

253056965 avatar Nov 21 '22 09:11 253056965

I couldn't find a solution. I'm using the separate layers. Joining the single layers until they form a group of complete layers.

Hi, can you tell me how you join these layers? Thanks!

CavellHuang avatar Mar 07 '23 12:03 CavellHuang

You will need to recursively go over the layers of the top node of the group you want to save and save each one of them over the parent node's image.

Example:


 PSD.open('caminho do psd').then( async function (psd) {
        /** Get info layer */
        const node = psd.tree().descendants();
        const contentInfo = await getinfoLayers(node);
 )}
 
 
 const getinfoLayers = async (node) => {   
    let content = []; // save all layers of each group
    let contentGroup = []; // save all layer groups
    let nameGroup = 0; // rename layer group
    let lastnameGroup = 1; // rename layer
    const fileLayers = join(__dirname,"../layers-all/") // base path

    const searchForImage = async (node) => {
	// does node contain children?
        if (node && node.hasChildren()) {
            nameGroup++;
            lastnameGroup = 1;
            if(nameGroup > 1){
                content.push([...contentGroup]);
            }
            contentGroup = [];
            await searchForImage(node.childeren);    
        } 
        else if (node) {        
            /** retun a Promise */
            // save layer to path...
            node.layer.image.saveAsPng(fileLayers+`${nameGroup}_${lastnameGroup}.png`);	
                    
            /** set infor layers */      
            contentGroup.push({
                name: `${nameGroup}_${lastnameGroup}`,
                height: node.get('height'),
                width: node.get('width'),
                opacity: node.export().opacity,
                coords: node.get('coords'),
                text: node.export().text ? node.export().text : ''
            });          
            lastnameGroup++;
        }
    }
    
    await Promise.all( node.map( node => searchForImage(node)));

    content.push([...contentGroup]);

    console.log("Finish get infomations the layers...");
    return content;
};

Another example: issue #143

After that you will have all the layers identified by group, that is: (01_01, 01_02, 02_01....).

Now you have the position information for each layer and can use some mechanism to join them together. It can be a library or canvas.

Allyson-Santana avatar Mar 08 '23 17:03 Allyson-Santana