Page 1 of 1

Problem using swap command

Posted: 2011-08-04T14:26:03-07:00
by Random
This is my first post here, so, hi to everybody and thanks to the creators of Imagemagick, it's such an impressive tool.

I'm having problems sortening layers using convert tool.

Basically, I start with an image and I want to do these manipulations:
  • Resize -> layer 0
  • Color multiply (duplicating the image, filling it with color and finally compositing) -> layer 1
  • Add border to layer 0
  • Shadow of layer 0 (duplicating it, converting into shadow and sending it to the bottom) -> layer 2
The problem is I am not able to sort properly the layers. Shadow layer appears to stay on top, no matter what I do. I can make it work with shadow or with coloring but I'm not able to make them work at same time. I've tried a lot of things but nothing seems to work. Here is a visual explanation:

Image

Here is my commandline:

Code: Select all

convert.exe image.png
            -matte -background none 
            -resize 300x270! 
            -unsharp 1.5x1+1+0.2
            ( +clone -fill "#bfae3b" -colorize 100% -compose Multiply )
            -bordercolor "#ffffff" -border 5
            -virtual-pixel transparent +distort SRT 0                                      <---- rotation, forget it ;)
            +repage ( +clone -shadow 0.5x3+0+3 )
            -swap 2,0 -swap 2,1 -flatten
            output.jpg
Any idea or suggestion about how to solve this problem?

Thanks in advance.

Re: Problem using swap command

Posted: 2011-08-04T15:53:42-07:00
by fmw42
png (and jpg) images do not support layers.

I am not sure what you are doing. If you resize one image (layer), then are you going to resize the others. If not, then the images will not overlap properly.

This line makes no sense. You cannot composite one image.

( +clone -fill "#bfae3b" -colorize 100% -compose Multiply )

Furthermore, you left out the -composite.

If you want to composite the clone with the previous image, then remove the -compose multiply -composite from within the parenthesis.

Best way to proceed would be to separate the steps into separate command lines until you get the process down. Show us the results of each step. Then try to combine the steps into one command line.

What is your starting image and do you have a Photoshop result you are trying to match in IM? Perhaps that will help us understand your processing and how to do it in IM.

Re: Problem using swap command

Posted: 2011-08-04T17:11:56-07:00
by anthony
Would be nice to have a link to the starting image so we can actually 'play' with it ;-)

Code: Select all

-swap 2,0 -swap 2,1 
can be replaced by -reverse.
http://www.imagemagick.org/Usage/basics/#reverse

NOTE watch your image sizes! A shadow image is by its definition larger than the original image, and will typically have a negative offset, so as to align the shadow with the original image! I recommend using -layers merge instead of -flatten. This looks for the smallest offset and subtracts it from all the images in the sequence, before flattening (using a size that contain all images). That is it will subtract, the negative shadow offset (making it an addition) and shift all the images down and right by the appropriate amount, so as to accommodate the generated shadow.

See generating shadows...
http://www.imagemagick.org/Usage/blur/#shadow

Re: Problem using swap command

Posted: 2011-08-05T03:54:54-07:00
by Random
Short answer: Problem solved, it happens a lot of times, you are lost a long time and when you finally ask for help you find the solution really quick. Thanks anyway ;)

Long answer:

fmw42, sorry if I didn't explain it right. I want to work with layers but finally I flatten the image. The image conversion is a part from a bigger python script so I've made a mistake, the final output image actually is a png, so it supports transparency.

About composition, I was working in IM like in Photoshop: every layer has its own composition method and you get your final image overlaying them. It seems IM works in a different way, you can only have ONE composition method active for all the layers (As you've pointed in your answer fmw42). This way, if I want to use different composition methods, I need to work step by step merging layers.

So, the final process is (I've removed certain parts for clarity):

Code: Select all

convert.exe original.png
       -matte -background none                                        ; first image (original) until here 
       ( +clone -fill "#bfae3b" -colorize 100%)                       ; second image (color layer) creation.
       -compose Multiply                                              ; Set Multiply mode for layer merging
       -layers merge                                                  ; At this point I have the colorized version of the original image as a single layer
       -bordercolor white -border 5                                   ; Border added to the base image (layer 0)
       ( +clone -shadow 0.5x4+0+2 )                                   ; Second image (shadow) creation  
       +swap                                                          ; Only 2 layers, so simply swapping them to put the shadow at the bottom 
       -compose Over                                                  ; Change Multiply by simple overlay (Over)       
       -layers merge                                                  ; Final result                      
       output.png                                                     ; Output file in png format to keep shadow transparency
anthony, thanks and done ;).

One thing I think is very interesting about working with layers is the command -append. Yesterday I was lost mainly because I didn't see the result of my different swap and merge commands. Simply deleting the layer compositions and addind -append at the end of the script I got a vertical strip containing all the different layers. This way you can check your layer order and if its content is what you expected or not.

Here is the final result (adding an extra background and also modifying brightness/contrast wich are no included in the above script):

Image

Again, thanks a lot for your answers and information.

Re: Problem using swap command

Posted: 2011-08-07T05:09:37-07:00
by anthony
Actually you don't even need to remove the later commands, but can just insert a debugging line to make a clone of all images, append them, -write it to show: and delete it!

Code: Select all

  \( -clone 0--1 +append -write show: +delete \) \
More debuging methods like this, and the one mentioned by Random are listed in IM Examples, Basics.
Complex Image Processing and Debugging
http://www.imagemagick.org/Usage/basics/#complex

If you come up with a nice debug method, please let me know!

Re: Problem using swap command

Posted: 2011-08-08T04:44:41-07:00
by Random
Thanks, Anthony, I've just started to use IM and I haven't seen before that information about debugging.

Regards.

Re: Problem using swap command

Posted: 2011-08-08T17:06:59-07:00
by anthony
Over the years a lot of information has been added. It is amazing just what people have come up with over the years.