Trouble Creating PSD with Layers

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
rlandrum
Posts: 9
Joined: 2011-11-04T17:32:10-07:00
Authentication code: 8675308

Trouble Creating PSD with Layers

Post by rlandrum »

I'm attempting to create a PSD from the command line on Linux with IM 6.5.7. I have 6 layers that I'd like to create. I've found examples of how to do this, but when I try them, the output is not what I expect. Specifically, -size seems to get ignored, and the layers are too large for the canvas size.

Example:

Code: Select all

convert -size 600x600 -alpha set piece-0.tif -alpha set piece-1.tif -alpha set piece-2.tif -alpha set piece-3.tif -alpha set piece-4.tif -adjoin test.psd
test.psd canvas size is only as large as piece-0.tif.

I've found that creating a background image of the canvas size helps, but the results are slightly unpredictable.

Code: Select all

convert -size 600x600 xc:white bg.tif
convert -alpha opaque bg.tif -alpha set piece-0.tif -alpha set piece-1.tif -alpha set piece-2.tif -alpha set piece-3.tif -alpha set piece-4.tif -adjoin test.psd
In this case, the first 100 or so pixels of bg.tif are white. and the remainder is mostly transparent, but with random lines of white through it. I assume this is probably a color palette merge issue though. This is almost usable, but the pieces are all stacked atop each other. I'd like to stagger them across the image, one about every 100 px.

I found this among the examples, but got only segfaults and errors when adjusting it for my needs. I'm a bit clueless here.

Code: Select all

convert -page +0+0  bg.tif \
        -page +5+20    piece-0.tif \
        -page +100+20  piece-1.tif \
        -page +200+20 piece-2.tif \
        -page +300+20 piece-3.tif \
        +page \
        -adjoin test.psd
This produces a file, but gimp fails to load it complaining about unsupported compression mode 65280.

I'm stumped, and I've been playing with it for hours trying to get it to do what I want.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble Creating PSD with Layers

Post by fmw42 »

try

convert piece-0.tif piece-1.tif piece-2.tif piece-3.tif piece-4.tif -alpha set test.psd

This will create a psd file with 5 layers. But generally the first layer of a psd file is the flattened result from all layers.

convert piece-4.tif piece-3.tif piece-2.tif piece-1.tif piece-0.tif \
\( -clone 0 -clone 1 -clone 2 -clone 3 -clone 4 -background white -flatten \) \
-alpha set -reverse test.psd
rlandrum
Posts: 9
Joined: 2011-11-04T17:32:10-07:00
Authentication code: 8675308

Re: Trouble Creating PSD with Layers

Post by rlandrum »

That works pretty well... Thanks... However, I'm still having trouble positioning my layers. There is clearly something I'm not getting about this. It would seem fairly obvious from the various examples given, but it just isn't working for PSDs.

Code: Select all

convert -size 100x400 xc:white -fill yellow -draw 'rectangle 20,20 80,380' piece-0.tif
convert -size 100x400 xc:white -fill red -draw 'rectangle 20,20 80,380' piece-1.tif
convert -size 100x400 xc:white -fill green -draw 'rectangle 20,20 80,380' piece-2.tif
convert -size 100x400 xc:white -fill blue -draw 'rectangle 20,20 80,380' piece-3.tif
convert -size 100x400 xc:white -fill pink -draw 'rectangle 20,20 80,380' piece-4.tif

convert \
\( -geometry +10+10 piece-4.tif -transparent white  \) \
\( -geometry +110+10 piece-3.tif -transparent white \) \
\( -geometry +210+10 piece-2.tif -transparent white \) \
\( -geometry +310+10 piece-1.tif -transparent white \) \
\( -geometry +410+10 piece-0.tif -transparent white \) \
-alpha set \
\( -size 600x600 xc:white -transparent purple \) \
-reverse test.psd
This should, in theory, place each generated rectangle image on the white 600x600 canvas spaced 100 pixels apart. Instead, it drops them on top of each other. I've tried -page, -repage, and just about everything else that looks like it might control the position of a layer within a larger document.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble Creating PSD with Layers

Post by fmw42 »

try this

convert -size 100x400 xc:white -fill yellow -draw 'rectangle 20,20 80,380' piece-0.tif
convert -size 100x400 xc:white -fill red -draw 'rectangle 20,20 80,380' piece-1.tif
convert -size 100x400 xc:white -fill green -draw 'rectangle 20,20 80,380' piece-2.tif
convert -size 100x400 xc:white -fill blue -draw 'rectangle 20,20 80,380' piece-3.tif
convert -size 100x400 xc:white -fill pink -draw 'rectangle 20,20 80,380' piece-4.tif

convert \
\( -page +10+10 piece-4.tif -transparent white -background none -mosaic \) \
\( -page +110+10 piece-3.tif -transparent white -background none -mosaic \) \
\( -page +210+10 piece-2.tif -transparent white -background none -mosaic \) \
\( -page +310+10 piece-1.tif -transparent white -background none -mosaic \) \
\( -page +410+10 piece-0.tif -transparent white -background none -mosaic \) \
\( -clone 0 -clone 1 -clone 2 -clone 3 -clone 4 -background white -mosaic \) \
-reverse test.psd


I believe that -page only works to set the virtual canvas when used in conjunction with one of the layers methods as described in
http://www.imagemagick.org/Usage/layers/

-mosaic is a canvas expanding method
rlandrum
Posts: 9
Joined: 2011-11-04T17:32:10-07:00
Authentication code: 8675308

Re: Trouble Creating PSD with Layers

Post by rlandrum »

The -mosaic is getting me very close. Unfortunately it changes the size of each of my layers. My actual image is 4000x4000, which means lots of very huge layers.

Maybe what I'm trying to do is too complicated. Is there anyway to move a layer once an image is created? I've looked, but I'm not seeing how to specifically address a layer within an image.

I created my image (with the pieces stacked), and edited the image with gimp. I moved each 100x400 layer to the correct position. I then saved it and used identify -format "%g" example.psd[3] and saw 100x400+45+20, which is great. It would be great if I could do something like convert example.psd[3] -geometry +45+20 to move that layer to the correct position. Alas, that did not work.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble Creating PSD with Layers

Post by fmw42 »

Sorry, you are getting beyond my understanding. I am not following what you need. Perhaps post a PS example from your test images above.

Other than that, you may need to wait for Anthony to reply. He may know what you need to do with IM.
rlandrum
Posts: 9
Joined: 2011-11-04T17:32:10-07:00
Authentication code: 8675308

Re: Trouble Creating PSD with Layers

Post by rlandrum »

Here's an example of what I'd like to be able to generate automatically.

Note how each layer is 100x400 except for the bottom layer, which (canvas layer) which is 600x600. Each piece is position at a different offset (spaced away from 0x0).

http://xjguy.com/example.psd (110K)

In the solution you provided, each layer started at 0x0, but had grown to include pieces at the correct offset positions. Basically, with hundreds (thousands) of layers, and an image of 4000x4000, I'm going to have some memory issues, I believe.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble Creating PSD with Layers

Post by fmw42 »

Apart from the fact that your image is 600x600, has reversed color order and has a white layer; whereas mine is missing the white layer and is 520x420, I cannot see what the difference is that you refer to? I must admit I know little about PSD files and have limited experience with PS. How can I tell from PS what the difference is that you refer to?

Your image seems to be using background transparency and mine has an alpha channel to deal with transparency!

NOTE that IM cannot do background transparency. It only knows about alpha channel transparency. Is that the problem?
rlandrum
Posts: 9
Joined: 2011-11-04T17:32:10-07:00
Authentication code: 8675308

Re: Trouble Creating PSD with Layers

Post by rlandrum »

Forgive me for not stating the objective from the start... the finished product is not the image, but a PSD file so that a human being can manipulate the layers in such a way as to form an image. Imagine the pieces to be puzzle pieces, and the PSD file will have once piece in each layer so that the human may move each layer to the correct place within the puzzle until the puzzle is complete and a more perfect picture is formed.

The layers of the image are the most important aspect of my challenge. The actual pieces vary in size, but have generally rectangular shapes. I need those pieces imported in their native sizes, and positioned on top of a white background so that they generally do not overlap (the color is unimportant really, but not transparent or checkerboard). The pieces, however, should have transparent backgrounds, so that it's possible to fit them together (they are irregularly shaped, pretty much exactly like real puzzle pieces).

I hope this clarifies my question some. I've been at this too long and apologize for not being more direct about the finished product from the start.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble Creating PSD with Layers

Post by fmw42 »

this is the best I have been able to do so far


convert \
\( -page +410+10 piece-0.tif -background none -mosaic \) \
\( -page +310+10 piece-1.tif -background none -mosaic \) \
\( -page +210+10 piece-2.tif -background none -mosaic \) \
\( -page +110+10 piece-3.tif -background none -mosaic \) \
\( -page +10+10 piece-4.tif -background none -mosaic \) \
\( -size 600x600 xc:white -clone 0 -clone 1 -clone 2 -clone 3 -clone 4 -mosaic \) \
-reverse -crop 600x600+0+0 +repage test3.psd


In PS, you don't see the first layer which is the composite. But if you export it as

convert test3.psd[0] test3-0.png

you will see that it has all the colored pieces and a white background
rlandrum
Posts: 9
Joined: 2011-11-04T17:32:10-07:00
Authentication code: 8675308

Re: Trouble Creating PSD with Layers

Post by rlandrum »

I'm unable to make this work using the perl API either. I've looked at the source. The PSD specification says that the layer positioning expects top, left, bottom, right as the layer location, but IM is providing width, height, x, y from a struct called RectangleInfo. That doesn't seem correct, especially since it's reading x, y, height, width from the MSB and subtracting x from width and y from height. I believe this explains why it's always coming up 0x0.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble Creating PSD with Layers

Post by fmw42 »

rlandrum wrote:I'm unable to make this work using the perl API either. I've looked at the source. The PSD specification says that the layer positioning expects top, left, bottom, right as the layer location, but IM is providing width, height, x, y from a struct called RectangleInfo. That doesn't seem correct, especially since it's reading x, y, height, width from the MSB and subtracting x from width and y from height. I believe this explains why it's always coming up 0x0.

I am not following all this as I don't know the PSD specs nor how to determine what is going on.

For me this seems to produce something like what you want when viewed in PS, except the white layer is coming out red. So I think that part is a bug.


convert -size 100x400 xc:none -fill yellow -draw 'rectangle 20,20 80,380' piece-0.tif
convert -size 100x400 xc:none -fill red -draw 'rectangle 20,20 80,380' piece-1.tif
convert -size 100x400 xc:none -fill green -draw 'rectangle 20,20 80,380' piece-2.tif
convert -size 100x400 xc:none -fill blue -draw 'rectangle 20,20 80,380' piece-3.tif
convert -size 100x400 xc:none -fill pink -draw 'rectangle 20,20 80,380' piece-4.tif
convert \
\( -page +410+10 piece-0.tif -background none -mosaic \) \
\( -page +310+10 piece-1.tif -background none -mosaic \) \
\( -page +210+10 piece-2.tif -background none -mosaic \) \
\( -page +110+10 piece-3.tif -background none -mosaic \) \
\( -page +10+10 piece-4.tif -background none -mosaic \) \
\( -size 600x600 xc:white \) \
\( -clone 5 -clone 0 -clone 1 -clone 2 -clone 3 -clone 4 -mosaic \) \
-reverse -crop 600x600+0+0 +repage test4.psd
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trouble Creating PSD with Layers

Post by anthony »

rlandrum wrote:The -mosaic is getting me very close. Unfortunately it changes the size of each of my layers. My actual image is 4000x4000, which means lots of very huge layers.

Maybe what I'm trying to do is too complicated. Is there anyway to move a layer once an image is created? I've looked, but I'm not seeing how to specifically address a layer within an image.

I created my image (with the pieces stacked), and edited the image with gimp. I moved each 100x400 layer to the correct position. I then saved it and used identify -format "%g" example.psd[3] and saw 100x400+45+20, which is great. It would be great if I could do something like convert example.psd[3] -geometry +45+20 to move that layer to the correct position. Alas, that did not work.

You can use the various Image Sequence Operators
http://www.imagemagick.org/Usage/basics/#sequence
to move the various layers around relative to each other. that is what they are for.

To re-position individual layers layers, you would generally make a 'clone' of the layer, and then modify the image meta data using -repage +X+Y. You can even move it relative to its previosu position by adding a '!'. Once done swap the modified version with teh original layer and delete the old one.

Code: Select all

   convert ...   \( -clone 5  -repage +100+100 \) -swap 5 +delete  ....
This is VERY memory efficient as the image data is NOT modified, only the images meta-data (virtual canvas offset) and thus the image continue to share the same data with its original. However modifying the image data using -crop, -border, etc will use more memory.

Also see Modifying Animations, Frame by frame modification
http://www.imagemagick.org/Usage/anim_mods/#frame_mod

You can of course use -page to set the image virtual canvas offset, as it is read in, though -page does not provide any form of relative adjustment, which -repage +X+Y\! can.

Note that with virtual canvas images. You can keep the image trimmed of excess background detail. You do not have to waste image space for it.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble Creating PSD with Layers

Post by fmw42 »

Anthony,

If I understand you, then this should work, but looking in Photoshop, none of the layers are offset properly (and the white layer is red).


convert -size 100x400 xc:none -fill yellow -draw 'rectangle 20,20 80,380' piece-0.miff
convert -size 100x400 xc:none -fill red -draw 'rectangle 20,20 80,380' piece-1.miff
convert -size 100x400 xc:none -fill green -draw 'rectangle 20,20 80,380' piece-2.miff
convert -size 100x400 xc:none -fill blue -draw 'rectangle 20,20 80,380' piece-3.miff
convert -size 100x400 xc:none -fill pink -draw 'rectangle 20,20 80,380' piece-4.miff
convert piece-0.miff piece-1.miff piece-2.miff piece-3.miff piece-4.miff \
\( -clone 0 -repage +410+10 \) \
\( -clone 1 -repage +310+10 \) \
\( -clone 2 -repage +210+10 \) \
\( -clone 3 -repage +110+10 \) \
\( -clone 4 -repage +10+10 \) \
\( -size 600x600 xc:white \) \
-delete 0-4 \
\( -clone 5 -clone 0 -clone 1 -clone 2 -clone 3 -clone 4 -mosaic \) \
-reverse test6.psd

This is a screen snap of this result:
http://www.fmwconcepts.com/misc_tests/p ... cture1.jpg

This is a screen snap of the result earlier (using -page):
http://www.fmwconcepts.com/misc_tests/p ... icture.jpg
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trouble Creating PSD with Layers

Post by anthony »

When generating the 'flattened' first image, I would use -flatten rather than -mosaic, so as to ensure that the image remains the same size as that first initial canvas. EG; in your example...

Code: Select all

convert
  -page +410+10 piece-0.miff \
  -page +310+10 piece-1.miff \
  -page +210+10 piece-2.miff \
  -page +110+10 piece-3.miff \
  -page +10+10 piece-4.miff \
  \( -size 600x600 xc:white \) -insert 0 \
  \( -clone 0-5 -flatten \) -insert 0
  test6.psd 
It may be that PSD images do not understand 'offsets'. I have not really had a lot of dealings with them, but all the ones I have seen have had no offsets in them.

If that is the case then you may need to 'fill out' the image canvas with transparency, and prehaps
also ensures all layers are cropped correctly.

One method of filling out the canvas is actually to miss-use the animation operator -coalesce. For example add this any either before or after the 'flatten image creation' step (make no difference).

Code: Select all

  ... -set dispose Background  -coalesce  ....
It is very tricky, and is exampled on...
Layering Images, Coalesce, a Progressive Layering
http://www.imagemagick.org/Usage/layers/#coalesce

Note that the logical operator to fill out the canvas like this is -extent, but it uses gravity/geometry offsets not layer/page offsets.

Let me know if this is what is required for PSD images, and I will note it in the PSD specific format area.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply