• Stars
    star
    2,680
  • Rank 16,383 (Top 0.4 %)
  • Language
    CoffeeScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Photoshop PSD file parser for NodeJS and browsers

PSD.js

Build Status Help Contribute to Open Source

A general purpose PSD parser written in Coffeescript. Based off of PSD.rb. It allows you to work with a Photoshop document in a manageable tree structure and find out important data such as:

  • Document structure
  • Document size
  • Layer/folder size + positioning
  • Layer/folder names
  • Layer/folder visibility and opacity
  • Font data (via psd-enginedata)
    • Text area contents
    • Font names, sizes, and colors
  • Color mode and bit-depth
  • Vector mask data
  • Flattened image data
  • Layer comps

Runs in both NodeJS and the browser (using browserify). There are still some pieces missing that are present in PSD.rb, such as layer comp filtering, a built-in renderer, and many layer info blocks. The eventual goal is full feature parity with PSD.rb.

Installation

PSD.js has no native dependencies. Simply add psd to your package.json or run npm install psd.

Documentation

Note: work in progress

Annotated source code documentation is available here. PROTIP: if you're wondering how to access various metadata from a layer, you'll want to see this file.

Usage

PSD.js works almost exactly the same in the browser and NodeJS.

NodeJS Example

var PSD = require('psd');
var psd = PSD.fromFile("path/to/file.psd");
psd.parse();

console.log(psd.tree().export());
console.log(psd.tree().childrenAtPath('A/B/C')[0].export());

// You can also use promises syntax for opening and parsing
PSD.open("path/to/file.psd").then(function (psd) {
  return psd.image.saveAsPng('./output.png');
}).then(function () {
  console.log("Finished!");
});

Browser Example

var PSD = require('psd');

// Load from URL
PSD.fromURL("/path/to/file.psd").then(function(psd) {
  document.getElementById('ImageContainer').appendChild(psd.image.toPng());
});

// Load from event, e.g. drag & drop
function onDrop(evt) {
  PSD.fromEvent(evt).then(function (psd) {
    console.log(psd.tree().export());
  }); 
}

Traversing the Document

To access the document as a tree structure, use psd.tree() to get the root node. From there, work with the tree using any of these methods:

  • root(): get the root node from anywhere in the tree
  • isRoot(): is this the root node?
  • children(): get all immediate children of the node
  • hasChildren(): does this node have any children?
  • childless(): opposite of hasChildren()
  • ancestors(): get all ancestors in the path of this node (excluding the root)
  • siblings(): get all sibling tree nodes including the current one (e.g. all layers in a folder)
  • nextSibling(): gets the sibling immediately following the current node
  • prevSibling(): gets the sibling immediately before the current node
  • hasSiblings(): does this node have any siblings?
  • onlyChild(): opposite of hasSiblings()
  • descendants(): get all descendant nodes not including the current one
  • subtree(): same as descendants but starts with the current node
  • depth(): calculate the depth of the current node (root node is 0)
  • path(): gets the path to the current node

If you know the path to a group or layer within the tree, you can search by that path. Note that this always returns an Array because layer/group names do not have to be unique. The search is always scoped to the descendants of the current node, as well.

psd.tree().childrenAtPath('Version A/Matte');
psd.tree().childrenAtPath(['Version A', 'Matte']);

Accessing Layer Data

To get data such as the name or dimensions of a layer:

node = psd.tree().descendants()[0];
node.get('name');
node.get('width');

PSD files also store various pieces of information in "layer info" blocks. See this file for all of the possible layer info blocks that PSD.js parses (in LAYER_INFO). Which blocks a layer has varies from layer-to-layer, but to access them you can do:

node = psd.tree().descendants()[0]
node.get('typeTool').export()
node.get('vectorMask').export()

Exporting Data

When working with the tree structure, you can recursively export any node to an object. This does not dump everything, but it does include the most commonly accessed information.

console.log(psd.tree().export());

Which produces something like:

{ children: 
   [ { type: 'group',
       visible: false,
       opacity: 1,
       blendingMode: 'normal',
       name: 'Version D',
       left: 0,
       right: 900,
       top: 0,
       bottom: 600,
       height: 600,
       width: 900,
       children: 
        [ { type: 'layer',
            visible: true,
            opacity: 1,
            blendingMode: 'normal',
            name: 'Make a change and save.',
            left: 275,
            right: 636,
            top: 435,
            bottom: 466,
            height: 31,
            width: 361,
            mask: {},
            text: 
             { value: 'Make a change and save.',
               font: 
                { name: 'HelveticaNeue-Light',
                  sizes: [ 33 ],
                  colors: [ [ 85, 96, 110, 255 ] ],
                  alignment: [ 'center' ] },
               left: 0,
               top: 0,
               right: 0,
               bottom: 0,
               transform: { xx: 1, xy: 0, yx: 0, yy: 1, tx: 456, ty: 459 } },
            image: {} } ] } ],
    document: 
       { width: 900,
         height: 600,
         resources: 
          { layerComps: 
             [ { id: 692243163, name: 'Version A', capturedInfo: 1 },
               { id: 725235304, name: 'Version B', capturedInfo: 1 },
               { id: 730932877, name: 'Version C', capturedInfo: 1 } ],
            guides: [],
            slices: [] } } }

You can also export the PSD to a flattened image. Please note that, at this time, not all image modes + depths are supported.

png = psd.image.toPng(); // get PNG object
psd.image.saveAsPng('path/to/output.png').then(function () {
  console.log('Exported!');
});

This uses the full rasterized preview provided by Photoshop. If the file was not saved with Compatibility Mode enabled, this will return an empty image.

More Repositories

1

CamanJS

Javascript HTML5 (Ca)nvas (Man)ipulation
HTML
3,527
star
2

CoffeeDrop

CoffeeDrop is an open-source, roll your own, Dropbox-like clone written in CoffeeScript. Currently not working and under heavy development.
CoffeeScript
510
star
3

ajax-chosen

A complement to the jQuery library Chosen that adds ajax autocomplete
CoffeeScript
409
star
4

flickr-store

Store arbitrary data on Flickr
Ruby
275
star
5

git-lfs-s3

A Git LFS server that uses S3 as the storage backend.
Ruby
216
star
6

CamanJS-Plugins

Plugins for CamanJS, a Javascript image manipulation library
CoffeeScript
195
star
7

RubyDrop

A roll your own Dropbox-like clone written in Ruby.
Ruby
126
star
8

nanocrawler

Web-based front end for viewing information about your Nano node and exploring the Nano network
JavaScript
83
star
9

CamanJS-Site

CamanJS Homepage
JavaScript
66
star
10

node-activerecord

A ORM written in Coffeescript that supports multiple database systems (SQL/NoSQL) and ID generation middleware.
CoffeeScript
57
star
11

JSONP-Fu

A Javascript library that provides access to multiple JSONP API's through a unified, extensible, and easy to use interface.
JavaScript
27
star
12

node-iracing

iRacing SDK bindings for NodeJS
C++
22
star
13

png-encode

Encode/decode arbitrary files using the PNG file format
Ruby
14
star
14

coffeescript-module

A base class for your Coffeescript projects
CoffeeScript
14
star
15

bindata.js

A structured and understandable way to read/write binary data in Javascript. Inspired by Ruby BinData.
CoffeeScript
10
star
16

Osimo-BBCode-Editor

An easy to use jQuery plugin BBCode editor
JavaScript
9
star
17

CamanJ

Java port of the CamanJS image manipulation library
Java
8
star
18

CamanDrop

Easy and automatic image filters using CamanJS, watch folders, and NodeJS.
JavaScript
7
star
19

NodeMonitor

A distributed server monitoring system written in Javascript for Node.JS that uses WebSockets to show server info in near real-time from the browser.
JavaScript
7
star
20

Asyrch

Asynchronous web searching using web workers and ajax
JavaScript
6
star
21

iracing-websocket-server

A WebSocket based server that streams data from iRacing to the browser.
JavaScript
6
star
22

qr-logo

A simple library for customizing a QR code with a logo.
JavaScript
5
star
23

campfire-notifier

A self-hosted Campfire push notification service
Ruby
4
star
24

MeltingIce-File-System

An online storage service with support for multiple users, Flash file uploading, and advanced user management. Project is no longer under active development.
PHP
4
star
25

wiki-philosophy

How many clicks does it take to get from any word to Philosophy on Wikipedia?
JavaScript
4
star
26

nano-seeds.lol

A take on keys.lol, but for Nano based currencies
JavaScript
4
star
27

nano-ipc-js

Javascript library for interacting with the Nano node IPC
JavaScript
4
star
28

Notetakr

An online note-taking application - no longer hosted, code is for experimentation and learning. This was a very old project of mine and is no longer under development.
JavaScript
3
star
29

TransmissionRemote-Pebble

Check on the status of your Transmission torrents from your Pebble smartwatch
JavaScript
3
star
30

meltingice.github.com

Github homepage
JavaScript
3
star
31

Osimo-BBCode-Parser

The BBCode parser made for Osimo Forum System, but can be used for any project.
PHP
3
star
32

node-memcached

A Node.JS memcached interface
3
star
33

Caman-C

C port of the Javascript image manipulation library CamanJS
C
3
star
34

Osimo-Forum-System-v2

Osimo is a forum system written from the ground up to be easy to theme and use.
PHP
3
star
35

nano-grpc

A gRPC server for the Nano cryptocurrency node
JavaScript
2
star
36

jSearch

A jQuery plugin to search an HTML element for a query and highlight the matches. Also, has a iPhone bookmarklet to search the current page and auto search engine detection.
JavaScript
2
star
37

nanocrawler-phoenix

Elixir/Phoenix based version of Nanocrawler, a Nano network explorer
JavaScript
1
star
38

nano-node-statsd

StatsD data collection for Nano currency nodes
JavaScript
1
star
39

deviant

An elasticsearch backed exception logging service
Ruby
1
star
40

cs-terrain-generator

A fractal terrain generator made especially for Cities: Skylines.
JavaScript
1
star
41

nano-ipc-rest

A simple IPC + REST RPC server for the Nano cryptocurrency node.
JavaScript
1
star
42

poopelo

Checks to see if our neighborhood pool is closed.
JavaScript
1
star
43

Osimo-Forum-System-v1

The old version of the Osimo Forum System that has been migrated to GitHub for archival purposes mostly. Please visit the Osimo Version 2 page to see the latest development!
PHP
1
star
44

Ghetto-FBChat

Back before Facebook supported its chat feature on mobile devices, I began to create a simple website that would allow this functionality without using the Facebook API. Chances are this code doesn't even work anymore, so it's here mostly for viewing pleasure.
JavaScript
1
star