Convert PSD to PNG with Node.js

By  on  

Automating and manipulating media is a fascination of mine, partly because I don't understand the magic behind it and partly because the idea of turning one thing into another is fun and useful.  That latest media tool that has piqued my interest is a JavaScript tool called psd.js.

psd.js is a project that allows you to read PSD files, including:

  • Document structure and size
  • Layer/folder size + positioning, names, 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

What the media converter and JavaScript lover in me found most awesome was one basic feature: converting a PSD to PNG with JavaScript!

var PSD = require('psd');
 
PSD.open('homepage.psd').then(function (psd) {
  return psd.image.saveAsPng('homepage.png');
}).then(function () {
  console.log('Finished!');
});

That's a nice, tidy API there and I love that it doesn't require anything other than JavaScript (many other Node.js image libraries require ImageMagick on the machine).  Of course converting PSD to PNG is easy with ImageMagick too, but being able to use Node.js instead opens a whole host of opportunity!

Recent Features

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

Incredible Demos

  • By
    Introducing MooTools ElementSpy

    One part of MooTools I love is the ease of implementing events within classes. Just add Events to your Implements array and you can fire events anywhere you want -- these events are extremely helpful. ScrollSpy and many other popular MooTools plugins would...

  • By
    Image Manipulation with PHP and the GD Library

    Yeah, I'm a Photoshop wizard. I rock the selection tool. I crop like a farmer. I dominate the bucket tool. Hell, I even went as far as wielding the wizard wand selection tool once. ...OK I'm rubbish when it comes to Photoshop.

Discussion

  1. Simon

    Wow I have been thrilled to learn about this cool feature and implemented a small Node binary to deal with PSD files

    For those running Linux/Mac who are tired of opening a VM and/or cracking Photoshop just to be able to see the new web design you need to integrate… Just install this module :

    npm install -g psd-cli

    You are then ready to see the real rendered PSD in a PNG file, without lousy Gimp changes by typing :

    psd my-file.psd -o

    Link to the NPM package : https://www.npmjs.com/package/psd-cli

  2. Thanks for the shout out David! I know you’re primarily focused on Javascript, but for completeness sake I figured I would mention that the Ruby version of the library is currently more feature complete: https://github.com/layervault/psd.rb

    I am planning on porting the rest of the features in the Ruby library to the JS version in the near future though!

  3. This is super! just been looking at jspdf also, Great what can be done on the client side these days.

  4. Doron

    Ryan, it’d be cool to see feature parity with the Ruby version! I wonder how much support is currently provided for working with PSD layers, e.g. applying transformations such as shadows and overlays (See: https://support.cloudinary.com/hc/en-us/articles/115001217949 for examples).

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!