webtoon / psd

Fast zero-dependency PSD parser for the web and Node.js

Home Page:https://webtoon.github.io/psd

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to save a layer to a image file

velara3 opened this issue · comments

In the documentation it says you can export a layer to pixel data. How would you save that to a PNG? I'm using node js.

// Extract the pixel data of a layer, with all layer and layer group effects applied
var layerPixelData = await layer.composite();

After calling layer.composite() to extract the pixel data, you can either use the Canvas API (if you're in a web browser) or a library like pngjs to convert that to a PNG file.

  let layerPixelData = await layer.composite()

    let png = new PNG({ width: layer.width, height: layer.height })
    png.data = Buffer.from(layerPixelData);
    png.pack().pipe(fs.createWriteStream('out.png'))