psd.js
psd.js copied to clipboard
How can I save mask data as png?
I have created a PSD file like the one pictured below.

What we want to save this time is this mask data.

To save it, I created the following source code.
import { fromFile } from "psd"
import sharp from "sharp"
const psdData = fromFile("src/resource/test_psd.psd")
psdData.parse()
const treePSD = psdData.tree()
const mask = treePSD.children()[2].get("mask")
const imageData = treePSD.children()[2].get("image").maskData
sharp(imageData, {
raw: {
width: mask.width,
height: mask.height,
channels: 4
}
}).png()
.toFile("src/resource/test.png")
However, test.png was inverted to black and white.

I would like to know how to save without inverting black and white. Thank you in advance.