ImageMagick v6 Examples --
Multi-Image Layers

Index
ImageMagick Examples Preface and Index
Layers Introduction
Appending Images (-append)
Layering images with Composite (composite, -geometry)
Composite Images in Convert (-composite, -geometry)
Draw Multiple Images (-draw image)
Flatten Multiple Layers of Images (-flatten, page)
Canvas Expanding Mosaics (-mosaic, -page)
Layer Merging (-layers merge)
Examples of Layering images
Coalesce, a Progressive Layering (-coalesce)
Compose Methods and Layering (-compose)
Averaging Images Together (-average)
Overlaying multiple image onto each other to generate a later 'composite' is generally known as using image 'layering'. These examples involve the combining of multiple images into larger images. Especially with the main 'work horse' operator of image layering within IM, "Flatten" (see below).


Layering Images Introduction

As we have previously noted, ImageMagick does not deal with just one image, but a sequence or list of images. This allows you to use IM in two very special image processing techniques.

You can for example think of each image in the sequence as a single frame in time, so that the whole sequence can be regarded as being a Animation. This will be explored in other IM Example Pages. See Animation Basics.

Alternatively, you can think of each image in the sequence as Layers of a set of see-through overhead transparencies. That is, each image represents a small part of the final image. For example: the first (lowest) layer can represent a background image. Above that you can have a fuzzy see though shadow. Then the next layer image contains the object that casts that shadow. On top of this a layer with some text that is written on that object.

That is you have a sequence of images or 'layers' that build up the final image on piece at a time. Each image layer can be moved, edited, or modified completely separately from any other layer, and even saved into a multi-image file (such as TIFF:, MIFF: or XCF:) or as separate images, for future processing. And that is the point of image layering.

Only when all the image layers have been created do you Flatten, Mosaic, or Merge all the Layered Images into a single final image.


Appending Images

Appending is probably the simplest, of the multi-image operations provided to handle multiple images.

Basically it joins the current sequence of images in memory into a column, or a row, without gaps. No form of justification such as centering is (currently) provided, so images are aligned either along their left, or top edges, as appropriate. The "-append" option appends vertically, while the plus form "+append" appends horizontally.

For example here we append a set of letter images together, side-by-side, to form a fancy word, in a similar way that fonts letters are joined together.

  convert font_A.gif font_P.gif font_P.gif font_E.gif font_N.gif \
          font_D.gif font_E.gif font_D.gif +append  append_row.gif
[IM Output]

The above is similar (in a very basic way) to how fonts are handled. Unlike real fonts you are not limited to just two colors, but can generate some very fancy colorful alphabets from individual character images. Many of these 'image fonts' are available on the WWW for download. A very small set can be found in Anthony's Icon Library, in Fonts for Text and Counters, which is also where I found the above Blue Bubble Font.

Note also how the "+append" operator was done last, on the command line, after all the images that you want to append have been added to the current image sequence.

This is great for appending a label to an image, for example...

  convert rose:  -background LawnGreen label:Rose \
          -background white  -append append_label.jpg
[IM Output]

Note that the "-background" color was used to fill in any space that was not filled in. Of course if the all the images are the same width, no space will be left for this fill.

From IM v6.4.7-1 the "-gravity" setting can be used to specify how the images should be added together. As such in a vertical append, a setting of 'Center' will center the image relative to the final resulting image (so will a setting of either 'North' or 'South' though they will have the same effect.


  convert rose:  -background LawnGreen label:Rose \
          -background white -gravity center -append \
          append_center.jpg
[IM Output]

Naturally any 'East' gravity setting will align the images on the right side.

  convert rose:  -background LawnGreen label:Rose \
          -background white -gravity east -append \
          append_east.jpg
[IM Output]

Similar vertical alignment can be achieved when using "+append"

Before IM v6.4.7 it was much more difficult to align appened images, and generally involved using a "-flop" for right alignment. Or using "-extent" or "-border" to adjust the image width for centered aligned appends.

For example, this will work with an older 6.3.2 version of IM...

  convert rose:  -background SkyBlue label:Rose \
          -background White -gravity center -extent 200x0 \
          -append -trim +repage   append_center_old.jpg
[IM Output]

You can also use multiple append operations, in the same command without conflict or confusion as to the outcome of the operations (which was not the case before IM v6).

  convert font_{0,0,6,1,2}.gif +append  dragon_long.gif \
          -background none   -append   append_multi.gif
[IM Output]

We appended each row of images together, then appended a larger image below that. Very simple, and straight forward.

By using parenthesis, you can append just the numbers after the larger image. For example, here append all the numbers together, before appending them vertically to the dragon image we read in before the numbers.

  convert dragon_long.gif  '(' font_{0,0,6,2,9}.gif +append ')' \
          -background none   -append   append_parenthesis.gif
[IM Output]

The parenthesis in the above must be either quoted, or escaped with a backslashed ('\') when used with a UNIX shell, otherwise they will be interpreted by the shell as something completely different.

As only two images were involved we could have just added a "+swap" or "-reverse" instead of using parenthesis.

You can take this further to make a whole array of images, and build them either by rows, or by columns.

  convert \( font_1.gif font_2.gif font_3.gif +append \) \
          \( font_4.gif font_5.gif font_6.gif +append \) \
          \( font_7.gif font_8.gif font_9.gif +append \) \
          \( -size 32x32 xc:none  font_0.gif +append \) \
          -background none -append   append_array.gif
[IM Output]

Technically the first set of parenthesis is not needed, as no images have been read in yet, but it makes the whole thing look uniform and shows the intent of the command, in making an array of images.

See also Montage Concatenation Mode, for an alternative way of creating arrays of equal sized images.

The "-append" operator will only append the actual images, and does not make use the virtual canvas (image page) size of offset. However the virtual canvas information seems to be left in a funny state with the canvas sizes being added together and the offset set to some undefined value.

This may be regarded as a bug, and means either the input images or result should have the virtual canvas reset using "+repage", before saving, or using the image in operations where this information can become important.

This situation will probably be fixed in some future expansion of the operation. Caution is thus advised, especially if re-appending Tile Cropped images.


Layering Images with Composite

The normal method of combining two images together using ImageMagick is though the "composite" command. This command however is designed to only combine only two images. This of course does not stop you from using it to layer multiple images, one image at a time...

  convert -size 100x100 xc:skyblue composite.gif
  composite -geometry  +5+10 balloon.gif composite.gif composite.gif
  composite -geometry +35+30 medical.gif composite.gif composite.gif
  composite -geometry +62+50 present.gif composite.gif composite.gif
  composite -geometry +10+55 shading.gif composite.gif composite.gif
[IM Output]

As all input images are read in by ImageMagick BEFORE the output image is opened, you can output to one of the input images. This allows you to work on the same image over and over, as above, without problems.

Do not do this with a lossy image format like "JPEG" as the format errors are accumulative, and the base image will quickly degrade.

You can also resize the overlaid image as well as position it using the "-geometry" setting.

  convert -size 100x100 xc:skyblue comp_resize.gif
  composite -geometry 40x40+5+10  balloon.gif comp_resize.gif comp_resize.gif
  composite -geometry      +35+30 medical.gif comp_resize.gif comp_resize.gif
  composite -geometry 24x24+62+50 present.gif comp_resize.gif comp_resize.gif
  composite -geometry 16x16+10+55 shading.gif comp_resize.gif comp_resize.gif
[IM Output]

The "composite" command also has a few other advantages in that you can use to control the way the image is drawn onto the background with the "-compose" option, and its relative position is effected by the "-gravity" setting.

You can also "-tile" the overlay so that it will just cover the background image, without needing to specify tile limits. Something only available in when using "composite".

The big disadvantage with this method is that you are using multiple commands, and IM has to write-out the working image, either to a pipeline, or to disk, for the next command to read-in again.

To find more examples of using the "composite" command, to overlay images on top of other images, see "Annotating by Overlaying Images" and "Image Positioning using Gravity".


Composite Images in Convert

With ImageMagick version 6 the "-composite" operator, is now available as an 'image operator' within the "convert" command. For more details see Convert -composite Operator. This allows you to do the same as the above, but all in one command.

  convert -size 100x100 xc:skyblue \
          balloon.gif  -geometry  +5+10  -composite \
          medical.gif  -geometry +35+30  -composite \
          present.gif  -geometry +62+50  -composite \
          shading.gif  -geometry +10+55  -composite \
          compose.gif
[IM Output]

This first creates a Canvas Image which is "skyblue" in color, and then layers each of the later images onto that canvas at the given locations.

Now the "-geometry" is is a very special operator that not only sets an overlay position for the next "-composite" operation, it also "-resize" the last image (and only the last image) in the current image sequence.


  convert -size 100x100 xc:skyblue \
          balloon.gif  -geometry 40x40+5+10   -composite \
          medical.gif  -geometry      +35+30  -composite \
          present.gif  -geometry 24x24+62+50  -composite \
          shading.gif  -geometry 16x16+10+55  -composite \
          compose_resize.gif
[IM Output]

There are of course features in "composite", not available in any form of "convert" image overlaying. See the composite command summery.

Image positions are effected by "-gravity" for their relative placement, allowing you to position images relative to the center, or bottom or right edges of the image. However is not the case for the next set of image overlay options, which only allows absolute placement.


Draw Multiple Images

Also using "convert" you can also use "-draw" to draw the images onto the Canvas Image background.

  convert -size 100x100 xc:skyblue \
          -draw "image over  5,10 0,0 'balloon.gif'" \
          -draw "image over 35,30 0,0 'medical.gif'" \
          -draw "image over 62,50 0,0 'present.gif'" \
          -draw "image over 10,55 0,0 'shading.gif'" \
          drawn.gif
[IM Output]

You can of course also specify a resize for the overlaid image too..

  convert -size 100x100 xc:skyblue \
          -draw "image over  5,10 40,40 'balloon.gif'" \
          -draw "image over 35,30  0,0  'medical.gif'" \
          -draw "image over 62,50 24,24 'present.gif'" \
          -draw "image over 10,55 16,16 'shading.gif'" \
          drawn_resize.gif
[IM Output]

As of IM version 6 the position of drawn images are "-gravity" effected, just like text. But this is NOT true in the previous IM version 5.5.7.   See examples of this in "Image Positioning using Gravity".


Flatten Multiple Image Layers

The "-flatten" image list operator, is very similar to last few example sections.

It will basically "Alpha Compose" all the given images together to form one single image. However the image positions are specified using there current Virtual Canvas, or Page offset.

For example, here I create a nice canvas, and specify each of the images I want to overlay onto that canvas.

  convert -size 100x100 xc:skyblue \
          -fill dodgerblue -draw 'circle 50,50 15,25' \
          -page +5+10  balloon.gif   -page +35+30 medical.gif  \
          -page +62+50 present.gif   -page +10+55 shading.gif  \
          -flatten  flatten_canvas.gif
[IM Output]

However you don't need to create an initial canvas as we did above, you can instead let "-flatten" create one for you. The canvas color will be the current "-background" color, while its size is defined by the first images Virtual Canvas size.

  convert -page 100x100+5+10  balloon.gif   -page +35+30 medical.gif  \
          -page +62+50        present.gif   -page +10+55 shading.gif  \
          -background dodgerblue  -flatten  flatten_page.gif
[IM Output]

While the "-gravity" setting will effect image placement defined using "-geometry" settings, it will not effect image positioning using virtual canvas offsets set via the "-page" setting. This is part of the definition of such offsets. See Geometry vs Page Offsets for more details.

If placement with "-gravity" is need look at either the above multi-image composition methods, or the special Layers Composition method that can handle both positioning methods simultaneously.

If any image does not appear in the defined virtual canvas area, it will be either clipped, or ignored, as appropriate. For example here we used a smaller canvas size, causing the later images not to appear completely on that canvas.

  convert -page 75x75+5+10  balloon.gif   -page +35+30 medical.gif  \
          -page +62+50 present.gif   -page +10+55 shading.gif  \
          -background dodgerblue  -flatten  flatten_bounds.gif
[IM Output]

The normal use of "-flatten" is to merge multiple 'layers' of images together.

That is you can be generating various parts of a larger image, usally using Parenthesis to limit image operators to the a single 'layer' image being generated, and then flatten the final result together.

For example one typical use is to create a Shadow Image layer, onto which the original image is flattened. For example...

  convert balloon.gif \( +clone  -background navy  -shadow 80x3+5+5 \) +swap \
          -background none   -flatten   flatten_shadow.png
[IM Output]

Note that as I want the shadow under the original image, I needed to swap the two images place them in the right order.

Because the Virtual Canvas consists of just a size, the resulting image will be that size, but have no virtual canvas offset, as such you do not need to worry about any offsets present in the final image.

This use of the virtual canvas to define the canvas on which to overlay the image means you can use it to add a surrounding border to an image. For example here I set the an image's size and virtual offset to 'pad out' an image to a specific size.

  convert medical.gif -set page 64x64+20+20 \
          -background SkyBlue   -flatten   flatten_padding.gif
[IM Output]

Of course there are better ways to Pad Out an Image so that IM automatically centers the image in the larger area.

Strangely the exact same handling can be used to 'clip' or Crop an image to a virtual canvas that is smaller than the original image. In this case however you want to use a negative offset on the image, relative to its virtual canvas which is fixed to the virtual canvas origin, by its definition.

  convert  logo:  -set page 100x100-190-60  -flatten  flatten_crop.gif
[IM Output]

Of course a Viewport Crop would also do this better, without the extra processing of canvas generation and overlaying that "-flatten" also does. It also will not 'expand' the image itself to cover the whole viewport if the image was only partially contained in that viewing window.

The most common use of the "-flatten" operator is to set convert a background transparency into a specific color. That is overlay a single image on a background color of the same size as the original image. For example, lets add a colored background to the "number pad" we created above.

  convert append_array.gif  -background LimeGreen  -flatten  flatten_bgnd.gif
[IM Output]

Technically we could have also used "-mosaic" to set a background color, but "-flatten" is the more correct solution as it applies the canvas page information correctly.

If you are only wanting to replace the transparent background of multiple images, then you can use a zero width "-border" instead.

  convert append_array.gif -bordercolor Gold -border 0  border_bgnd.gif
[IM Output]

This is not a "-layers" operator so multiple images will be 'flattened' separatally, without them all 'flatten' down to a single image. See Border Examples for details, and Flatten Animations for a multi-image example.

As of IM v6.3.6-2 the "-flatten" operator is only an alias for a "-layers 'flatten'" method.

Thus the "-flatten" option can be regarded as a short cut for the "-layers" method of the same name.


Canvas Expanding Mosaics

While "-flatten" can be regarded as the main 'work horse' of image layering techniques. The "-mosaic" operator is more like a self-expanding version.

Rather than only creating an initial canvas based on just the canvas size of the initial image, "-mosaic" operator creates a canvas which is large enough to hold all the images.

For example here I don't even set an appropriate Virtual Canvas, however the "-mosaic" operator will work out how big such a canvas needs to be to hold all the image layers.

  convert -page +5+10  balloon.gif   -page +35+30 medical.gif  \
          -page +62+50 present.gif   -page +10+55 shading.gif  \
          -background dodgerblue  -mosaic  mosaic.gif
[IM Output]

Note that both "-mosaic" and "-flatten" still creates a canvas that started from the 'origin' or 0,0 pixel. This is part of the defination of an images 'virtual canvas' or 'page' and because of this you can be sure that the final image for both operators will have a 0 virtual offset, and the whole canvas will be fully defined in terms of actual pixel data.

Also note that "-mosaic" will only expand the canvas in the positive directions (the bottom or right edges), as the top and left edge are fixed to the virtual orign. that of course means "-mosaic" will still clip images with negative offsets...

  convert -page -5-10  balloon.gif   -page +35+30 medical.gif  \
          -page +62+50 present.gif   -page +10+55 shading.gif  \
          -background dodgerblue  -mosaic  mosaic_clip.gif
[IM Output]

As on IM v6.3.6-2 the "-mosaic" operator is only an alias for a "-layers 'mosaic'".

Thus the "-mosaic" option can be regarded as a short cut for the "-layers" method of the same name.


Layer Merging

The "-layers" method 'merge' is also very similar to the previous operators, and was added with IM v6.3.6-2. It only creates a canvas image just large enough to hold all the given images at there respective offsets, but will not clip images with negative offsets, or add extra space for images with large positive offsets.

The method makes a new virtual canvas layer of all the current existing layers, without any extra space. It also sets an virtual offset for the newly merged layer image, so the image remains correctly positioned, even if that means giving it a negative offset.

For example the first example of mosaic above will come out as...

  convert -page +5+10  balloon.gif   -page +35+30 medical.gif  \
          -page +62+50 present.gif   -page +10+55 shading.gif  \
          -background dodgerblue  -layers merge  +repage layers_merge.gif
[IM Output]

While any image with a negative offset will not be clipped, but will also be preserved.

  convert -page -5-10  balloon.gif   -page +35+30 medical.gif  \
          -page +62+50 present.gif   -page +10+55 shading.gif  \
          -background dodgerblue  -layers merge  +repage layers_merge_2.gif
[IM Output]

Note that I used of the "+repage" operator in the above examples to remove the virtual canvas offset that would have been generated to correctly position the layer in the virtual image space. I did this as web browsers often have trouble with image offsets unless part of a GIF animation.

This method can also be used to add a background color to a image with transparency, but without 'filling out' the whole virtual canvas, or changing the images virtual 'page' settings. This can be important if the image is to be further processed afterward.

It was primarily designed so that you can line up images that were either positioned, or distorted to matching locations (control points) so they can be joined to form a later image mosaic. Specifically so that it will do so without the user needing to worry about 'negate clipping', or extra space being added, during alpha composition.

For example, you can merge images that have been distorted using common control points, to create a 3D Isometric Photo Cube, or a 3D Perspective Box.

It can also be used in creating larger panoramic images from smaller ones. Example reference to come.


Programmed Positioning of Layered Images

Laying multiple images using the various operators above is a very versatile technique. It lets you work on a large number of images individually, and then when finished you combine them all together as a single simple step.

One simple use of this technique is demonstrated in Overlapping Photos for generating a simple series of overlapping images.

But you can do a lot more by combining image layering with various techniques generally used to Further Processing thumbnails. For example adding a Soft Edge to the images as you read and position them, you can generate a rather nice composition of images, on a Tiled Canvas.

  convert -page +5+5    holocaust_tn.gif \
          -page +80+50  spiral_stairs_tn.gif \
          -page +40+110 chinese_chess_tn.gif \
          +page \
          -matte -background None -virtual-pixel background \
          -channel A -blur 0x10  -level 0,50% +channel \
          -size 200x200 tile:tile_fabric.gif -insert 0 \
          -flatten  overlap_canvas.jpg
[IM Output]

You can however do a lot more. By separating the thumbnail processing and position, from the final layered image step, you can generate a programmed placement of images, in a vast variety of ways.

For example, lets generate create Polaroid Thumbnails from the larger original images in Photo Store, processing them individually.

The resulting thumbnails are actually variable in size due to the random rotation that the thumbnailing added. This means to properly position the image we need to read the resulting size and adjust the images location in the final image layers, so the centers remain equally spaced.

The script below does this very nicely, but thanks to the use of a pipelined concatenated MIFF: file format, only one temporary file is needed during the processing of each image. Future versions of IM may even eliminate that single temporary file, but for now that is a very small price (See Hints for Better Scripts).

And here is the resulting script...

  center=100   # Start position of the center of the first image

  for image in ../photo_store/[a-m]*_orig.jpg
  do
    # read, thumbnail, polaroid image to a temp file.
    convert -size 500x500 "$image" -thumbnail 240x240 \
            -set caption '%t' -bordercolor Lavender -background black \
            -pointsize 12  -density 96x96  +polaroid  -resize 30% \
            tmp.png

    # Now gather image size, and determine its virtual canvas location
    pos=`convert tmp.png -format "+%[fx: $center-w/2 ]-%[fx: h/2 ]" info:`

    # Position and output the generated thumbnail to the pipeline
    convert -page $pos  tmp.png  MIFF:-

    # update the center for the next image
    center=`convert xc: -format "%[fx: $center +60 ]" info:`
  done |
    # read pipeline of positioned images, and merge onto a canvas
    convert -background skyblue  MIFF:-  -layers merge +repage \
            -bordercolor skyblue -border 3x3   overlapped_polaroids.jpg

  rm -f tmp.png

[IM Output]

This may seem a complex script but it really isn't, though windows, and PHP users may have some work to do to convert it (see Windows and PHP Scripts). I have also expanded the above script and placed it in the Example Scripts area as "polaroid_series" for you to download and play with (please contribute additions and changes for others to use).

The above example will let you programmatically generate just about any pattern, of any set of images, in any style onto a virtual canvas.

The script could read style information for individual photos from some database, or use pre-generated thumbnails for the images. You do not need to figure out canvas size, or worry that your images having an overall negative offset.

Basically, a looped-pipeline method like the above will let you concentrate your efforts on style and arrangement (negative or otherwise) of the photos, without the overhead of canvas handling, which you can leave IM to sort out.


Coalesce, a Progressive Layering

The "-coalesce" image operator, is really designed for converting GIF animations into a sequence of images. For examples see Coalescing Animations for details. However it is very closely associated with "-flatten", and has very useful effects for multi-layered images in this regard.

For example using "-coalesce" on a single image, will do exact the same job as using "-flatten" with a "-background" color of 'None' or 'Transparency'. That is it will 'fill out' the canvas of the image with transparent pixels.

  convert  -page 100x100+5+10 balloon.gif -coalesce  coalesce_canvas.gif
[IM Output]

When dealing with a image consisting on multiple layers, "-coalesce" can be used to generate a 'Progressive Layering' of the image. But to do this we need to take a few precautions, to disable any 'GIF animation' handling by the operator.


   convert -page 100x100+5+10 balloon.gif   -page +35+30 medical.gif  \
           -page +62+50       present.gif   -page +10+55 shading.gif  \
           -set dispose None  -coalesce  miff:- |\
     montage - -frame 4 -tile x1 -geometry +2+2 \
             -background none -bordercolor none  coalesce_none.gif
[IM Output]

In the above we "-set" all the "-dispose" settings to 'None'. This effectively tells "-coalesce" to just overlay each frame on top the results of the previous overlays.

The result is the first image is just a 'fill out' of the images canvas, with a transparency background. The next image is the previous image with that layer overlaid. And so on. A 'progressive' flatten of the image sequence.

The last image in the sequence will thus be the same as if you did a normal "-flatten" with a transparent background.

You can get a completely different sort of effect if you had used a "-dispose" setting of 'Background'. In this case "-coalesce" will just 'fill out' the canvas of each image, as if they were completely separate images!

  convert -page 100x100+5+10 balloon.gif   -page +35+30 medical.gif  \
          -page +62+50       present.gif   -page +10+55 shading.gif  \
          -set dispose Background  -coalesce  miff:- |\
    montage - -frame 4 -tile x1 -geometry +2+2 \
            -background none -bordercolor none  coalesce_bgnd.gif
[IM Output]

If you are actually wanting to also replace the transparent background of the multiple images, then you can add a zero width border instead of attempting to overlay each image onto a background one at a time.

  convert -page 100x100+5+10 balloon.gif   -page +35+30 medical.gif  \
          -page +62+50       present.gif   -page +10+55 shading.gif  \
          -set dispose Background  -coalesce \
          -bordercolor skyblue  -border 0x0   miff:- |\
    montage - -frame 4 -tile x1 -geometry +2+2 \
            -background none -bordercolor none  coalesce_bgnd_border.gif
[IM Output]

Please note however that unlike "-mosaic" and "-flatten" or even "-border",   the "-coalesce" operator does not make use of the current "-compose" alpha composition setting. It only uses an 'Over' compose method, as this is what is hard coded internally.

Using different "-compose" methods with the more standard image layering operators is the subject of the next set of examples.


Compose Methods and Layering

The three "-layers" methods 'flatten', 'mosaic', and 'merge', as well as the shortcut operators, "-flatten" and "-mosaic", all will make use of the "-compose" setting to determine the composition method used to overlay each image in sequence. As such you could think of these functions as a multi-image "-composite" operator with the ability to set an initial "-background" canvas color.

However using anything but the default Alpha Composition of 'Over' requires a some thought before applying or you will get unexpected results. You may also need to turn off the "-background" canvas by setting it to the transparent color 'none' to ensure it does not interfere.

For example lets place each successive image under the previous images using a 'DstOver'...

  convert -page 100x100+5+10 balloon.gif   -page +35+30 medical.gif  \
          -page +62+50       present.gif   -page +10+55 shading.gif  \
          -background none  -compose DstOver  -flatten  flatten_dstover.gif
[IM Output]

Remember the background was set to be transparent, otherwise you will only see the background canvas in the result as all the other images will have been placed 'under' this initial canvas! Of course that presents a way of 'blanking' an image with a particular color. For more details see Canvases Sized to an Existing Image.

Here is a more practical example. Rather than layering the images with the background canvas first, which awkward and un-natural in some image processing situations, you can just generate the images top-down or foreground to background order.

  convert rose: -repage +10+10 \
          \( +clone -background black -shadow 60x3+5+5 \) \
          \( granite: -crop 100x80+0+0 +repage \) \
          -background none  -compose DstOver -layers merge layer_dstover.gif
[IM Output]

Each of the first three lines generates one layer image, with the final line merging all the layers under the previous layers, effectivally reversing the order.

As you can see the image processing for the above was simpler and cleaner than you normally would see with shadow generation, just by underlaying each image in sequence (with a transparent starting canvas)

Of course I could have just as easily Reverse the Image Sequence instead, which unlike using 'DstOver' in the above. This only reorders the existing images and not the extra 'internal background canvas' that the layering methods create. For example..

  convert rose: -repage +10+10 \
          \( +clone -background black -shadow 60x3+5+5 \) \
          \( granite: -crop 100x80+0+0 +repage \) \
          -reverse -layers merge layer_reverse.gif
[IM Output]

The compose methods can also be used to produce some interesting effects. For example, if you draw three circles, then by overlaying them using the 'Xor' compose method, you get a unusual and complex looking symbol, for minimal effort.

  convert -size 60x60 \
          \( xc:none -fill blue   -draw 'circle 21,39 24,57' \) \
          \( xc:none -fill red    -draw 'circle 39,39 36,57' \) \
          \( xc:none -fill green  -draw 'circle 30,21 30,3'  \) \
          -background none  -compose Xor   -flatten  flatten_xor.png
[IM Output]


Averaging Images Together

The "-average" image list operator will merge the current sequence of images together by averaging their values. However all images in the sequence being averaged together, must be the same size, or this operator will probably silently fail.

An alternative for averaging two images together is to use a "composite -blend 50%" image operation, which will work with two different sized images. See the example of Blend Two Images Together for more detail.

Matt Leigh, of the University of Arizona, reports that he has used "-average" to improve the resolution of microscope images. He takes multiple images of the same "target" then averages them all together to increase the signal/noise ratio of the results. He suggests others may also find it useful for this purpose.

As video sequences are notoriously noisy when you look at the individual frames, you can average a number of consecutive, but unchanging, frames together to produce produce much better result.



   -layers trim-bounds can be used to ensure all images get a positive
   offset on a minimal canvas size, while retaining there relative positions.
   The images are not merged.

   However if images have a transparency, it is suggested you trim that
   transparency first, making the ideal usage...

     -alpha set -bordercolor none -border 1x1 -trim -layers trim-bounds

   This minimizes the image layers including any and all transparent areas
   of actual image data, to ensure everything is contained on a valid virtual
   (positive) canvas of minimal size.


Created: 3 January 2004
Updated: 20 October 2008
Author: Anthony Thyssen, <A.Thyssen@griffith.edu.au>
Examples Generated with: [version image]
URL: http://www.imagemagick.org/Usage/layers/

a