composing more than 2 images

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
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

composing more than 2 images

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: composing more than 2 images

Post by magick »

We have a patch in ImageMagick 6.5.4-4 Beta, available sometime tomorrow, to fix the problem you reported. Thanks.
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: composing more than 2 images

Post by HugoRune »

Thanks!
Is there a "proper" way to do this, i.e. without reading the same file twice?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: composing more than 2 images

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: composing more than 2 images

Post 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).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply