Page 1 of 1

composing more than 2 images

Posted: 2009-07-21T12:24:08-07:00
by HugoRune
I am having some trouble with the -compose syntax, and need a hint.

Here is a modified example of what I want to do:

overlay an image with a vertically compresssed altered copy and a horizontal compressed altered copy of itself
Image --> Image

first attempt:

Code: Select all

convert ROSE.JPG 
    ( +clone   -scale 10x100%  +dither -colors 2 -equalize )  
    ( -clone 0 -scale 100x10%  +dither -colors 2 -equalize )  
    -gravity  center -compose copy -composite -composite ROSECROSS.JPG
This does of course not work: the first -composite uses the third image on the stack as a mask.
(Using -flatten instead of -composite works, but that ignores the gravity setting, and would not work if I wanted some other composition method like blend or multiply)

Code: Select all

convert ROSE.JPG 
    ( +clone   -scale 10x100%  +dither -colors 2 -equalize )  
    -gravity center -compose copy -composite 
    ( -clone 0 -scale 100x10%  +dither -colors 2 -equalize )  
    -composite ROSECROSS.JPG
This of course does not work either: the -clone 0 clones the already altered image, not the original.

Code: Select all

convert ROSE.JPG 
    ( +clone   -scale 10x100%  +dither -colors 2 -equalize )  
    -gravity center -compose copy -composite 
    ( ROSE.JPG -scale 100x10%  +dither -colors 2 -equalize )  
    -composite ROSECROSS.JPG
This seems inelegant, the source image has to be read twice. Nevertheless it should work, and it does indeed work if I use the internal "rose:" image, or a png. But if I use a JPG file, I get the error

Code: Select all

convert: Cannot quantize to fewer than 8 colors `ROSE.jpg' @ jpeg.c/EmitMessage/231.
convert: missing an image filename `ROSECROSS.JPG' @ convert.c/ConvertImageCommand/2772.

Re: composing more than 2 images

Posted: 2009-07-21T12:58:16-07:00
by magick
We have a patch in ImageMagick 6.5.4-4 Beta, available sometime tomorrow, to fix the problem you reported. Thanks.

Re: composing more than 2 images

Posted: 2009-07-21T13:34:38-07:00
by HugoRune
Thanks!
Is there a "proper" way to do this, i.e. without reading the same file twice?

Re: composing more than 2 images

Posted: 2009-07-21T13:47:55-07:00
by Bonzo
You can save the image into memory and use it as many times as you want:

Code: Select all

convert input.jpg -write mpr:image +delete ( mpr:image -thumbnail x480 -write 480_wide.jpg ) ( mpr:image -thumbnail x250 -write 250_wide.jpg ) ( mpr:image -thumbnail x100 -write 100_wide.jpg ) ( mpr:image -thumbnail 64x64! -write 64_square.jpg ) ( mpr:image -colorspace Gray -write black_white.jpg )
convert input.jpg -write mpr:image +delete
Reads the image into memory and calls it ( in this case ) image; you can then see how you can use the image over and over again.

Re: composing more than 2 images

Posted: 2009-07-21T19:23:03-07:00
by anthony
Or when you compose for the first time, make another clone of the original image,
Of course you need to delete images you no longer.

Code: Select all

  convert ROSE.jpg \
               \( .......... \) \
              \( -clone 0,1 -gravity center -compose copy -composite \) -delete 1 \
              \( -clone 0 ........ \) \
             -delete 0 -composite ROSECROSS.JPG
Alternative, do all operational steps in parenthesis (add more images), then delete all but the last at the end...

Code: Select all

convert ROSE.jpg \
             \( ........ \) \
             \( ......... \) \
             \( ......... \) \
             \( ..........\) \
             -delete 0--2  ROSECROSS.JPG
this is actually good for debugging as ALL the intermediate images remain in memory, And intermediate image keeps the same 'step number' that generated it.

You can also check the steps by removing the final delete (multi-image output), or replacing it with say a -append instead.

Or you could insert \( +clone -write x: +delete \)\ lines between steps to display the intermediate results.

In other words using parenthesis and cloning for ALL steps is a nice programming practice!

See Combining Image Sequence Operations OR the 'all in one' example in Multiplying Biased gradients where I have made use of exactly that technique (helped me a great deal when I was debugging this technique).