Skip to content

Please advise about replace an image within a Smart Object,help me! #241

Description

@826327700

I referred to this issue: #173 to implement the function of replacing smart object images. I have replaced the canvas and exported the modified PSD file. When I open this file in Photoshop, it already shows the modified image, which meets my expectations. However, when I export this PSD as a PNG, it appears unmodified. Why is this? Did I miss any steps? I ultimately need the PNG. Below is part of my code:

// Load new image and draw it onto the layer canvas
    const image = await loadImage('image.jpg');
    
    // Create a new canvas with the same size as the original layer
    if (targetLayer.canvas) {
      const width = targetLayer.canvas.width;
      const height = targetLayer.canvas.height;
      const newCanvas = createCanvas(width, height);
      const ctx = newCanvas.getContext('2d');
      
      // Draw the new image onto the canvas
      ctx.drawImage(image, 0, 0, width, height);
      
      // Replace the layer's canvas with the new one
      targetLayer.canvas = newCanvas;
    }
    
    // Export the modified PSD as a new PSD file
    const modifiedPsdBuffer = writePsdBuffer(psd);
    
    fs.writeFileSync('modified.psd', modifiedPsdBuffer);
    console.log('Exported modified PSD as modified.psd');
    
    const psd1 = readPsd(fs.readFileSync('modified.psd') as any,{ skipLayerImageData: false, skipCompositeImageData: false, skipThumbnail: false });
    // Export the entire PSD as PNG
    if (psd1.canvas) {
      const pngBuffer = psd1.canvas.toBuffer('image/png');
      fs.writeFileSync('output.png', pngBuffer);// ---> this output.png is not modified
      console.log('Exported entire PSD as output.png');
    }
    
    console.log('Smart object replacement completed!');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions