Giter VIP home page Giter VIP logo

psd's Introduction

@webtoon/psd

A lightweight Adobe Photoshop .psd/.psb file parser in typescript with zero-dependency for WebBrowser and NodeJS

Browser Support

Chrome Firefox Safari Internet Explorer
38 20 10.1 Not Supported

Installation

$ npm install @webtoon/psd

Benchmarks

You can run benchmarks for @webtoon/psd in your browser.

Features

โœ… Supported

  • Support large format (.psb)
  • Image / Layer information (size, offset, etc.)
  • Image / Layer pixel data
  • Unicode layer names
  • Image / Layer opacity
  • Text layers string value
  • Guides
  • Slices

๐Ÿšง Work in progress

  • Layer effects (shadow, overlays, etc.)

โŒ Unsupported

  • Photoshop metadata not directly related to the image

Usage

@webtoon/psd is provided as a pure ECMAScript module.

Web Browsers

Check out the live demo and the the source for web browser.

@webtoon/psd must be bundled with a bundler such as Webpack or Rollup.

@webtoon/psd reads a PSD file as an ArrayBuffer. You can use FileReader or File to load a PSD file:

import Psd from "@webtoon/psd";

const inputEl: HTMLInputElement = document.querySelector("input[type='file']");
inputEl.addEventListener("change", async () => {
  const file = inputEl.files[0];

  const result = await file.arrayBuffer();
  const psdFile = Psd.parse(result);

  const canvasElement = document.createElement("canvas");
  const context = canvasElement.getContext("2d");
  const imageData = new ImageData(
    psdFile.composite(),
    psdFile.width,
    psdFile.height
  );

  canvasElement.width = psdFile.width;
  canvasElement.height = psdFile.height;

  context.putImageData(imageData, 0, 0);
  document.body.append(canvasElement);
});

For performance, we recommend parsing PSD files in a Web Worker rather than the main thread.

NodeJS

@webtoon/psd does not support the Node.js Buffer. You must explicitly supply the underlying ArrayBuffer.

import * as fs from "fs";
import Psd from "@webtoon/psd";

const psdData = fs.readFileSync("./my-file.psd");
// Pass the ArrayBuffer instance inside the Buffer
const psdFile = Psd.parse(psdData.buffer);

Since @webtoon/psd is provided as an ES module, you must use dynamic import() or a bundler to run it in CommonJS code:

const Psd = await import("@webtoon/psd");

API Docs

This library provides the Psd class as the default export.

Opening a file

Psd.parse(ArrayBuffer) takes an ArrayBuffer containing a PSD or PSB file and returns a new Psd object.

const psdFile = Psd.parse(myBuffer);

Traversing layers

A Psd object contains a tree of Layer and Group (i.e. layer group) objects.

  • The Psd object provides a children property, which is an array of top-level Layers and Groups.
  • Each Group object provides a children property, which is an array of Layers and Groups that belong immediately under the current layer group .
import Psd, {Group, Layer, Node} from "@webtoon/psd";

// Recursively traverse layers and layer groups
function traverseNode(node: Node) {
  if (node instanceof Group) {
    for (const child of node.children) {
      traverseNode(child);
    }
  } else if (node instanceof Layer) {
    // Do something with layer
  } else {
    throw new Error("Invalid node type");
  }
}

for (const node of psdFile.children) {
  traverseNode(node);
}

The Psd object also provides the layers property, which is an array of all Layers in the image (including nested).

for (const layer of psdFile.layers) {
  doSomething(layer);
}

Retrieving image data

Use Psd.prototype.composite() and Layer.prototype.composite() to retrieve the pixel information for the entire image or an individual layer.

// Extract the pixel data of the entire image
pixelData = psd.composite();

// Extract the pixel data of a layer, with all layer and layer group effects applied
// (currently, only the opacity is supported)
layerPixelData = layer.composite();

// Extract the pixel data of a layer, with only the layer's own effects applied
layerPixelData = layer.composite(true, false);

// Extract the pixel data of a layer, without any effects
layerPixelData = layer.composite(false);

License

@webtoon/psd is released under the MIT license.

Copyright 2021-present NAVER WEBTOON

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

psd's People

Contributors

dlehdanakf avatar pastelmind avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.