21

I need to create a layered PSD file with ImageMagick or any other command-line tool available on Linux platform. Since I need to do this on Linux server, I can't use Photoshop scripting.

The ImageMagick command below creates PSD file with a single layer, where two images (here: plasma fractals) are positioned one below another. (I use ImageMagick 6.5.3-10 2009-07-31 Q16, latest available in MacPorts.)

convert -size 100x100 plasma:fractal plasma:fractal -append out.psd

How do I create a PSD file where each image is in its own layer, and one layer is directly above another?

Or, is there any other command line tool that would allow me to do this?

Update: Any other options than GIMP command line?

Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160

6 Answers6

16

If ImageMagick won't work, I'd look at Gimp command line.

The following commands created a 2-layer PSD file for me in the interactive console:

> (gimp-image-new 200 200 0)
(1)
> (gimp-layer-new 1 200 200 0 "layer-1" 100 0)
(2)
> (gimp-layer-new 1 200 200 0 "layer-2" 100 0)
(3)
> (file-psd-save 0 1 0 "test.psd" "test.psd" 0 0)
> (gimp-image-add-layer 1 2 -1)
> (gimp-image-add-layer 1 3 -1)
> (file-psd-save 0 1 1 "test.psd" "test.psd" 0 0)

That would need to be converted into a script-fu script (.scm file) and could be executed from the command-line with something like this:

gimp -i -b '(your-script-name "test.psd" 200 200)' -b '(gimp-quit 0)'
Jon Galloway
  • 52,327
  • 25
  • 125
  • 193
  • It's not that ImageMagick won't work, it's that I do not know how to make it work properly... To use Gimp is interesting idea, thank you. However, I'm a bit concerned with the need to install it on the server without X Window system and Gnome... Wouldn't it be a bit too heavy? – Alexander Gladysh Jul 25 '09 at 08:18
  • Gimp is definitely heavier than ImageMagick, and from what I read it's not going to perform as quickly, either. But from what I read of ImageMagick, I'm not sure it can create layered PSD's, just edit or flatten them. – Jon Galloway Jul 25 '09 at 18:25
9

I use the command lines below. I have not encountered any issue in opening the generated PSD in Photoshop, however every layer appears as a background layer, and you have to convert it into a true layer first in order to edit the layer ordering.

Here is the command line for Window. Given the list of images (im1.xxx, im2.xxx etc, im1 being the bottom layer,) a list of labels for the layers ("label1", "label2"...) :

convert ^ ( ^ -page +0+0 ^ -label "label1" ^ im1.xxx[0] ^ -background none ^ -mosaic ^ -set colorspace RGB ^ ) ^ ( ^ -page +0+0 ^ -label "label2" ^ "im2.xxx"[0] ^ -background none ^ -mosaic ^ -set colorspace RGB ^ ) ^ ( ^ -clone 0--1 ^ -background none ^ -mosaic ^ ) ^ -alpha Off ^ -reverse ^ "out.psd"

That is, for each layer, you have something like

( ^ -page +0+0 ^ -label "optional_label" ^ im1.xxx[0] ^ -background none ^ -mosaic ^ -set colorspace RGB ^ )

The label/name of the layer is optional (remove -label if none.) The [0] in im1.xxx[0] retrieves the first image in the image file, in case there exist a thumbnail in the Exif.

On Unix/OSX, you have to protect the parenthesis by a backslash, and the line continuation characters change also to \:

\( \ -page +0+0 \ -label "optional_label" \ im1.xxx[0] \ -background none \ -mosaic \ -set colorspace RGB \ \)

If the image names contain special chars, you can protect them with " (eg "c:\my im1.png") without any issue.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Raffi
  • 3,068
  • 31
  • 33
5

You can use the -adjoin to combine an image sequence.

convert -size 100x100             \
        -alpha set plasma:fractal \
        -alpha set plasma:fractal \
        -adjoin                   \
        out.psd
  • The alpha channels are needed for the PSD coder.
  • The order of the images is bottom layer to top layer.
  • There are a lot of compatibility issues with Photoshop and GIMP depending on the settings.

Using:

  • ImageMagick 6.5.4-6
  • Photoshop CS2
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Jared314
  • 5,181
  • 24
  • 30
  • Indeed there are some compatibility problems. Both Photoshop CS4 and OS X built-in previewer plainly refuse to open resulting image. :-( – Alexander Gladysh Jul 30 '09 at 06:03
  • Updated the command to work with Photoshop CS2, but I cannot test CS4 – Jared314 Jul 30 '09 at 17:02
  • Still does not work with neither CS4 nor CS3 nor OS X viewer. Photoshop says: "There was a problem reading the layer data. Read the composite data instead?" and then "Could not complete your request because the file is not compatible with this version of Photoshop. – Alexander Gladysh Jul 31 '09 at 07:06
  • Updated ImageMagick to 6.5.3-10 2009-07-31 Q16 (the latest available in MacPorts), but it still does not work. – Alexander Gladysh Jul 31 '09 at 07:26
4

Here is some useful links to you:

The second link is to use with PHP, but it executes ImageMagick, only use the commands, not the all PHP syntax, only the line of exec code.

Hope i'm helping you!

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
2

I agree with Jon Galloway, the Gimp console is a better choice. Here is my script:

(define (pngtopsd width height png-paths psd-path)
(define (add-layers image png-paths) 
    (if (null? png-paths) 0 
        (let* 
            ((png (car png-paths))
            (new-layer (car (gimp-file-load-layer 0 image (car png)))))

            (gimp-image-insert-layer image new-layer 0 -1)
            (gimp-item-transform-2d new-layer 0 0 1 1 (cadr png) (caddr png) (cadddr png))
            (add-layers image (cdr png-paths))
        )
    ))

(let* 
    ((png (car png-paths))
    (image (car (gimp-file-load 1 (car png) (car png))))
    (drawable (car (gimp-image-get-active-layer image))))

    (gimp-image-resize image width height 0 0)
    (gimp-item-transform-2d drawable 0 0 1 1 (cadr png) (caddr png) (cadddr png))       
    (add-layers image (cdr png-paths))
    (file-psd-save 0 image drawable psd-path psd-path 1 0)
    (gimp-image-delete image)
))

You just need put this script into file with name "pngtopsd.scm" inside your gimp "script" directory ("c:\Program Files\GIMP 2\share\gimp\2.0\scripts\" for Windows) and you can create layered PSD from list of PNG pictures with transformation (translation or rotation) of each layer. Usage sample:

gimp-console-2.8.exe -i -b              ^
  "(pngtopsd (list                      ^
   (list \"c:/../1.png\" 0 500 500)     ^
   (list \"c:/.../2.png\" 0.7 200 1000) ^
   (list \"c:/.../3.jpg\" -0.5 1000 0)) ^
   \"c:/.../result.psd\")"

There (list \"c:/.../2.png\" 0.7 200 1000) means:

  • 0.7 is the rotation angle of picture (in radians)
  • 200 1000 is x and y shift on an image
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • This is great! I've simplified it a bit, so it takes only a list of images. The width and height default to the size of the first image in the list, and all the layers are assumed to be aligned already, so you would invoke it as `(layers-to-psd '("background.jpg" "A.png" "B.png") "output.psd")`. Here's the gist: https://gist.github.com/endavid/fceea065e586052de944ffc57014692c – endavid Dec 18 '19 at 17:34
0

You can create a layered PSD with ImageMagick, but note the first image is actually used as the preview image (i.e. composite of all the layers).

convert -size 100x100 plasma:fractal plasma:fractal plasma:fractal out.psd

Will create a 2 layered PSD.

Petah
  • 45,477
  • 28
  • 157
  • 213