38

I'm using ImageMagick to extract layers from a PSD, and it gets them all out fine with:

convert image.psd image-%d.png

But the resulting PNG images are of varying dimensions, depending on the actual contents of the layer. What I'd like is to extract all the layers, but have them all the same size, so that I can easily lay them on top of each other later, and have everything line up just as it did in the original PSD.

If it helps to visualise it, this is what I'm currently getting with the command above:

+----+
|A   |   +-+   +-+
|    | = |A| + |B|
|   B|   +-+   +-+
+----+

And what I want is:

+----+   +----+   +----+
|A   |   |A   |   |    |
|    | = |    | + |    |
|   B|   |    |   |   B|
+----+   +----+   +----+

With the resulting images having a transparent background so that I can do this:

+----+   +----+ 
|A   |   |A   |+
|    | = |    ||
|   B|   |   B||
+----+   +----+|
          +----+

I'm not in any way tied to ImageMagick, so if there's another (preferably command-line) tool that can achieve this, that's fine.

Thom
  • 2,643
  • 2
  • 30
  • 33

3 Answers3

23

I use this command line to do what are describing:

convert.exe <filename>.psd -set dispose Background -coalesce <outfilename>.png
Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
killmenow
  • 231
  • 3
  • 3
9

Type this to extract the layer number 2 from a PSD file:

convert             \
  <filename>.psd[0] \
  <filename>.psd[2] \
  \(                    \
     -clone 0           \
     -alpha transparent \
  \)                    \
 -swap 0           \
 +delete           \
 -coalesce         \
 -compose src-over \
 -composite        \
 <extracted-filename>.png

This creates first a transparent canvas with the same size of the PSD file, then combines it with the layer 2 keeping it's original layout (-coalesce)

pacification
  • 5,838
  • 4
  • 29
  • 51
biondo
  • 151
  • 2
  • 3
  • 1
    Excellent! Is there any simple way to do this for all the layers at once? – Thom Apr 09 '15 at 07:48
  • 3
    To extract all layers to pngs keeping transparency, you can do something like: `for i in $(identify -format "%[scene] " .psd); do convert .psd[$i] -$i.png; done` At least in my version of imagemagick all pngs keep transparency, no need to add all the `convert` options (`ImageMagick 6.8.9-9 Q16 i686 2015-01-06`) – aesede May 07 '15 at 16:25
  • I tried this, but aside from layer 0 all I get are blank layers! (correct dimensions but entirely transparent). – Camilo Martin Apr 03 '21 at 10:00
2

In the end this is what worked for me:

convert -dispose Background "input.psd" -layers coalesce "output.png"

Credit goes to "snibgo" from the ImageMagick forums.

Camilo Martin
  • 37,236
  • 20
  • 111
  • 154