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

How can I save mask data as png?

Open Jamu2800 opened this issue 3 years ago • 0 comments

I have created a PSD file like the one pictured below. image

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

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. test

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

Jamu2800 avatar Jan 03 '23 11:01 Jamu2800