Cloning + composite?

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?".
Post Reply
juman
Posts: 6
Joined: 2017-10-22T11:08:36-07:00
Authentication code: 1151

Cloning + composite?

Post by juman »

I create an image with a red square on it like this :

Code: Select all

convert -size 4500x4500 xc:white ( -size 400x400 xc:red -geometry +500+900 ) -composite a_new.png
I then try to clone and rotate the square like this :

Code: Select all

convert -size 4500x4500 xc:white ( -size 400x400 xc:red -geometry +500+900 ) ( -clone -1 -rotate 45 ) -composite a_new.png
This just gives me a white image? However if remove the -composite I get three images. One white, one red square and one tilted red square so what am I doing wrong if I want them all in the same image?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cloning + composite?

Post by fmw42 »

First you should put the -geometry outside the parenthesis

Code: Select all

convert -size 4500x4500 xc:white ( -size 400x400 xc:red  ) -geometry +500+900 -composite a_new.png
Second, composite can only do two images at a time. So you need to specify geometry for both composites.

If you need to clone the second image for the using again, then you must clone the first to do the first composite and then at the end delete the originals.

You also probably do not want the minus sign for the clone.

So this creates the first two image. Then it clones them and composites them together. Then it clones the second image again. Then it composites that onto the previous composite and before removes the original two. If you want the two red squares to be centered on each other, then you need to use -gravity center and -geometry values relative to the center. Try these two to see.

Code: Select all

convert -size 4500x4500 xc:white ( -size 400x400 xc:red ) ^
( -clone 0 -clone 1 -geometry +500+900 -composite ) ^
( -clone 1 -background none -rotate 45 +repage ) ^
-delete 0,1 -geometry +500+900 -composite ^
a_new.png

Code: Select all

convert -size 4500x4500 xc:white ( -size 400x400 xc:red ) ^
( -clone 0 -clone 1 -gravity center -geometry +500+900 -composite ) ^
( -clone 1 -background none -rotate 45 +repage ) ^
-delete 0,1 -gravity center -geometry +500+900 -composite ^
a_new.png

See http://www.imagemagick.org/Usage/layers/#convert
juman
Posts: 6
Joined: 2017-10-22T11:08:36-07:00
Authentication code: 1151

Re: Cloning + composite?

Post by juman »

Thanks for the feedback. I don't fully follow though why you need to delete something?

What I want to do is create a canvas, create a image and position it on the canvas, clone the image and position it on another place on the canvas and then save the image. Is there maybe a simpler way to achieve this?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cloning + composite?

Post by fmw42 »

Once imagemagick does as composite, the original images are freed from memory. So you have to keep them around so that you can clone one of them and do a second composite. Once you have set up the second composite with the cloned image, you have to free the first two otherwise composite has too many images in the command line sequence, since it only allows 2.

You can overlay several images at one time using the -flatten (-layers flatten) command, but you have to specify the offset before the images using -page or you can specify them afterwards using -set page.

Try

Code: Select all

convert ( -size 4500x4500 xc:white -set page +0+0 )  ^
( -size 400x400 xc:red -set page +500+900 )  ^
( -clone 1 -background none -rotate 45 +repage -set page +1000+1800 )  ^
-flatten a_new.png
Post Reply