ImageMagick v6 Examples --
Alpha Compositing

Index
ImageMagick Examples Preface and Index
Alpha Composition in IM
Composite Geometry/Gravity Settings   vs   Layer Canvas/Page Offsets
Duff-Porter Compose Methods
OverDst OverSrcCopyReplace
DstIn,  Dst In, & Out,  Dst Out
ATopDst ATop,  ClearXor
Mathematical Compose Methods
MultiplyScreenBumpmapDivide
PlusAddMinusSubtractDifferenceExclusion
Color Manipulation Methods
OverlayHard LightSoft Light
LightenDarkenColor DodgeColor BurnLinear Light
Channel Coping Methods
Copy Opacity,     Copy RedCopy GreenCopy Blue
Copy CyanCopy MagentaCopy YellowCopy Black
HueSaturateLuminizeColorize
Special Composition Methods
Change MaskOther Specials
Dissolve one Image Over Another
Blend two images together
Watermark
Using a Mask to Limit Composed Area
Composite using Image Tiles
Raw Tables of Compose Methods (Separate Page)

Alpha Compositing is the technique of combining images, (with or without transparency, or alpha channels) together in some way. This is generally performed using the "composite" IM command, though it is also used by other image operators.


Alpha Composition in IM

Alpha Composition is the process of merging two (or more) images together in various ways. It describes what the results should be and whether the result should be transparent or not. That is, just how is the alpha or transparency channel to be handled.

A good introduction is SVG Compositing in 30 Minutes. Or you can find out the exact mathematical details by looking at the SVG Alpha Compositing Page.

Alpha composition is used in a lot of different places in ImageMagick, either directly or indirectly as part of an image operator's internal processing. The follow commands are the direct image 'composite' methods available in ImageMagick...

  composite {overlay} {background} [{mask}]  [-compose {method}]   {result}

  convert  {background} {overlay} [{mask}] [-compose {method}] -composite   {result}

The "composite" command is the traditional and normal method of compositing in IM. However the other two work exactly the same way, allowing more flexible image operations using the "convert" command.

Note the reversal of the order of the images when using "convert". This is for practical reasons, as you generally are working with a background image, over which you then want to prepare and overlay a new working image. You can use the Swap Image List Operator to swap images in a "convert" image processing sequence, before composing them.

Examples of using all of the above techniques (and more) can be seen in given in Layers of Multiple Images.

When "Alpha Compositing", the overlay image is also often called the source image, and the background image, the destination. This is good to keep in mind when looking these image operations, as many are described in terms of source and destination images.

The important point however is that, within IM, the final result image gets its size information from the background or destination image. This means you can not generally just swap the image arguments (and compose methods) to achieve the same result, unless the two images are the same size.

Apart from the 'direct' two image compositing styles shown above, there are a a number of other image operations that also use alpha compositing as part of their internal image processing. These include...
Layering Operators
Which overlay all the given images onto an initial canvas. Layering methods include include "flatten", "mosaic", and the new "-layers merge".

Edge Expansion Operators
Which overlays each individual image on a canvas of a specific size and decoration. This includes operators such as "-border", "-frame", and "-extent".
The "montage" program
The "-frame" montage setting makes use of the previous image framing operator, to frame the list of images before positioning them on the background canvas. More specifics can be found in Montage Background and Transparency.

Draw Images
The "-draw 'image...'" image overlay method, will let you overlay a single external source image over multiple destination images.

It is the only image composition method available to "mogrify" due to this 'external source image' limitation.

Multi-Sequence Layers Composite
The "-layers composite" method will let you compose two separate multiple image sequence together to form a new multi-image sequence. It also allows you to use either a single source (like "-draw") or a single destination image, against a sequence of images.

Also it will handle the both a universal "-gravity" effected "-geometry" offsets, as well as virtual canvas "-page" offsets for individual image pairs. It is the ONLY composition operator to allow both positioning techniques.

This Alpha Composition method is especially important for composing Image Animations, or other image sequences, as a whole, rather than needing a script to process each pair of images at a time.

The actual method of "Alpha Compositing" (both directly and indirectly) is controlled by the "-compose" setting, which defaults to a value of 'Over'. That is the source image is drawn over the background image.

Unfortunately other than using the 'Over' compose method, the other methods are a mystery to most users of IM. Here I will trying to de-mystify them, showing you just how useful they can be.

I will not go though ALL the compose methods, only ones I can show a practical example for that method. If you are interested in basic table of the results of these compose methods, see Tables of Compose Methods. Note that these tables don't really show the methods intended use, just the raw output from various test images.

The compose method is case-insensitive and the '_' or '-' characters are optional. As such the compose method "Dst_Over" can also be specified as: "dst_over", "dst-over", "DstOver", "dstover", or even "dstOVER". They all mean the same thing.

Also the compose methods: 'Over', 'ATop', 'In' and 'Out' are actually short for the more verbose compose method names: 'Src_Over', 'Src_ATop', 'Src_In' and 'Src_Out'.


Composite Geometry/Gravity Settings
          vs   Layer Canvas/Page Offsets

In normal Alpha Composition the "-geometry" setting is used with "-gravity" to position the source image relative to the destination image. Using these settings is covered in great detail in Image Positioning using Gravity. As such I refer you to there.

Note however that the size component of "-geometry" is special in that it will Resize a Image (geometry). In "composite", it resizes the source image, but for "convert" the last image in the image sequence is resized. This aspect is technically a separate aspect to Image Composition. See Geometry Resize, and Layering images with Composite.


However Layered Images composition methods allow the use of a virtual canvas "-page" or Virtual Canvas image meta-data for positioning the source image relative to the destination image (which may also have a virtual canvas offset).

This offset however is NOT gravity effected. But as each individual image can have a separate 'canvas offset' in its meta-data, it is much better suited for the composition of multiple images using Image Layering Operators, as well as Multi-Image Sequence Composition for animations.

The two styles are very different positioning techniques, and it is important you use the right style for the composition techniques you plan to use.


Note that one composition technqiue allows you to use BOTH positioning methods simultaniously, the multi-image sequence Layers Composition operator. The layer offsets being used for individual images, while composite geometry position (with gravity) is used for global positioning of the two image sequences based on their virtual canvas size.


Duff-Porter Compose Methods

The Duff-Porter image composition methods are a traditional set of 12 methods which are very well defined. You can find the mathematical definitions for these in the SVG Alpha Compositing document.

All these 12 methods define not only which of the two images are 'on top' but also how the alpha, or transparency of the each image effects the result. The default compose method 'Over', is just one of these 12 Duff-Porter methods, but you can do so much more that just that.

Here is a classical table showing the results of these 12 methods with two triangular images.
[IM Output]

To understand the above compose methods, the resulting image would be the same as if you said...
{Source} --{compose_method}--> {background}

That is the resulting image for 'ATop' is equivalent to "the source image 'ATop' of the background image". Just remember in "composite" the images are source then background or destination, but in "convert" and other IM commands, the background or destintion image is given first, then the source image to be overlaid.

In the above table I made the destination image larger than you would typically see in a Duff-Porter Composition Table. This allows you to see what happens to areas which aren't overlaid by the source image. Some methods clear the areas that are not overlaid, while others preserve the background image in these areas. These 'clearing' methods are: 'Src', 'In', 'Dst_In', 'Out', 'Dst_ATop', and 'Clear'.

ImageMagick version 5 and before did not clear any area not overlaid by the source image. This was incorrect handling of the Duff-Porter Composition operators, and was corrected during IM v6 development.

On top of these, a thirteenth Duff-Porter style compose method has been added for the special needs of ImageMagick. This is the 'Copy' compose method, is exactly the same as a 'Src' method (overwrite the background image completely), but which does not clear the background outside the areas the source image overlays. In other words it copies not only the source image pixels, but also any transparency it defines, but ONLY in that area overlaid. For more details see the 'Copy' examples below.

Over (overlay image over background)

This is the default compose method, the one everyone thinks of when overlaying an image on top of another image. The overlay image is placed 'over' the background image, in the same way as a 'animation cell' or 'overhead transparency' can be placed over a background scene or image.

It is so common that I doubt I really need to say much more. So lets present a example of overlaying a single letter image over a background image.

  convert -background none  -fill white \
          -font Ravie  -pointsize 36   label:A   label_A_white.png
  composite -gravity center label_A_white.png   rose:   compose_over.gif
[IM Output]
[IM Output]

The overlay can be centered with gravity as above, or positioned exactly using a "-geometry" setting. If it is positioned outside the bounds of the background it will be clipped, or ignored, as appropriate.

For example here we overlaid the image using the "convert -composite" operator this time, but half way off the background. Note the reversal of the image order for the "convert" form of alpha composition.

  convert  rose:   label_A_white.png \
          -geometry +5-15   -composite   convert_over.gif
[IM Output]

Dst_Over (put image 'under' background)

Like other 'Dst_*' methods, this works almost exactly like the 'Src_' version of the method (just remove the "Dst" from the method name), but with the overlay and background images swapped. As such 'Dst_Over' is equivalent to placing the source overlay image 'under' the destination. The result is that only the parts of our source or overlay image not hidden by the destination or background image will be visible as they peek out from underneath.

This is NOT exactly the same as 'Over' with argument images swapped, as the size, positioning and other image meta-data, still comes from the background or destination image. As such the output image size will set by the now foregrounded 'background' image.

For example, here we overlay a black A label 'under' our original white A. We can reposition the black A, relative to the white A background. As you can see this is great for adding a hard shadow, without needing to worry about expanding the image size.

  convert -background none  -fill black \
          -font Ravie  -pointsize 36   label:A   label_A_black.png
  composite -compose Dst_Over  -geometry +5+5 \
          label_A_black.png  label_A_white.png   compose_under.png
[IM Output]
[IM Output]

The important thing to note is that the size of the background is preserved while the overlays position relative to the background can be modified, and is clipped by the background. This is what makes this operator such a important and useful operator to know.

The "-tile" setting of "composite" also makes this form of 'over' very useful as a way to tile underneath a image. For example lets tile the built in checkerboard pattern underneath the shadowed letter we just created.

  composite  -compose Dst_Over -tile pattern:checkerboard \
             compose_under.png   compose_under_tiled.jpg
[IM Output]

This technique is especially important when the image your are wanting to view has semi-transparent colors, as many display programs (or even web browsers) do not handle semi-transparent colors properly.

Src ('crop' to background image dimensions)

This operator just completely replaces the background image with the overlay image. The colors and transparency in the background is completely junked, leaving a blank image the same size as the original destination, over which to apply the source image.

But, what good is that?

Well as the resulting image has the same size as the background, a lot of use. You can effectively use this operator to either crop or add a border to the overlay image so that becomes the same size as the given background image!

For example suppose you want to make a plasma image and clip it so it is the same size at the built-in rose image. This will let you do it, without needing to know just how big the rose image really is.

  composite -compose Src  -size 100x100  plasma:tomato-dodgerblue \
            rose:   compose_crop.gif
[IM Output]

Of course the above is equivalent to a "-crop" of the plasma image, not a 'resize'.

If the overlay is smaller, then transparent space is added around the overlay to fill it out. Please note however that the background must have an alpha channel, or it will fill the extra space with black (the color that 'none' or 'transparent' has without any alpha channel).

  composite -compose Src -gravity South \
            hand_point.gif   rose:   -matte   compose_expand.gif
[IM Output]
[IM Output]

Copy (copy or replace image simply)

This operator is not strictly part of the 12 'Duff-Porter' compose methods, (which is why it was not on the chart above), but it is very important nevertheless. It works exactly like the "Src" compose method, but it does not touch the background image outside the overlaid area. That is, it limits its coping abilities to just the area overlaid and nothing else.

This allows you to crop out a small section of a large image, work on the smaller image for speed, then 'copy' the results back onto the larger image without touching the other areas of that image. This in turn allows you to optimize image processing of very very large images.

It's being used internally by IM for operations like "-append" and "-draw", for exactly this reason. You will also need to use this method, when you want to completely overwrite the background image with a "-tile" image. (See Composite Tile Examples for details).

Here is the same example I used above, but using 'Copy' instead of 'Src', showing how IM will not waste time clearing out the rest of the background as demanded by the 'Duff-Porter' algorithm.

  composite -compose Copy -gravity South \
            hand_point.gif   rose:   -matte   compose_copy.gif
[IM Output]

This operator is also very similar to 'Over', except that the transparency of the source image is also copied, replacing the background image completely. If their is no transparency, it would then be exactly like 'Over'.

Dst (a 'no-op' compose)

This operator basically does absolutely nothing. The source, or overlay image is completely ignored, and the destination or background image is left completely unchanged.

The methods real use, is as a means of 'turning off' a alpha composition operation, in other image operators.

For example here we use the "-frame" operator to frame our rose image (with a transparent background) but then used a "-compose Dst" to prevent the image from being added to the frame. In other words, we only used the rose to set the size of the internal frame in the result. The frame and only the frame was the result.

  convert rose:   -matte  -mattecolor Gold  -bordercolor none \
          -compose Dst   -frame 7x7+3+2   compose_frame_dst.gif
[IM Output]

The "-matte" operator in this command is needed to ensure an alpha channel is available, or you will end up with a black middle, rather than a transparent one. The "-bordercolor" in turn defines the color inside of the frame which is usually placed underneath the source image.

The 'Dst' method can also be useful in a script to disable an alpha composition in a large an complex command, without needing to create two different IM commands.

Dst_In (or 'mask' the background with source)

The 'Dst_In' method is like using the source image as a 'Copy_Opacity' mask for the background image. It will cut out the overlay images shape from the background image, like a cookie cutter which cuts out a cookie shapes from cookie dough.

For example lets use the fancy 'A' from above as a mask to cut out its shape from the rose image.


  composite -compose Dst_In  -gravity center \
            label_A_white.png  rose: -matte  compose_mask.png
[IM Output]

Unlike 'Copy_Opacity' method, you can NOT use a greyscale image as the mask, as only the overlay images alpha channel is used in this operation. Any color in the overlay is completely ignored.

Dst_Out (or a 'erase' operation)

Using the 'cookie dough' analogy of 'Dst_In', the result of the 'Dst_Out' method is the dough that was left behind once a cookie has been cut out.

It can be used to cut holes, or take bites out of the background image using the shape of the overlay. Any color in the overlay is again completely ignored.


  composite -compose Dst_Out  -gravity center \
            label_A_white.png  rose: -matte  compose_erase.png
[IM Output]

The mathematical formulae of these two compose methods are designed explicitly so that if you use 'Dst_In' and 'Dst_Out' on the same set of images, you can fit them back together (using 'Plus' method) just like a jigsaw puzzle.

For example here we 'add' (using 'Plus' composition) the last two images we generated above. This image is exactly the same (to the pixel) as the original 'rose:' image.

  composite -compose Plus  compose_mask.png  compose_erase.png \
            compose_rejoin.png
[IM Output]

Erasing part of an image is not an easy task in any graphics program. For example a "-draw" operation can only add color to an image. It will not remove color once it has been applied to your canvas. Think of a painter who is painting some advertising on a glass door or window, he can add paint, but he can't remove paint, by painting over it.

By drawing the shape you what to erase onto a transparent canvas, you can then use 'Dst_Out' to then remove it from your working image. It's a bit like our painter carefully wiping off wet paint with a specially shaped rag. The shape can be used to remove all, or some of the color, making it either fully or semi-transparent, as the case may be.

For example suppose you wanted to draw a crescent moon symbol on a transparent canvas. This alpha composition method makes this difficult shape, easy to draw, by just overlaying two circles.

  convert -size 70x70 xc:none -fill white -draw 'circle 35,35 35,5' \
          \( -size 70x70 xc:none -fill black -draw 'circle 28,30 35,5' \) \
          -matte -compose Dst_Out  -composite   moon_crescent.png
[IM Output]

The second image must be created and drawn in parenthesis. If you don't you will find that "-draw" will draw onto BOTH the overlay image AND your original background, which is obviously wrong.

If you did not use parenthesis, you will find a circle of semi-transparent black pixels around the erased part of the image. I know it happened to me while creating this example, much to my own annoyance.

ATop ('Over', but clip to background image)

Like 'Over' but limit the result to the original shape of the background image. In other words the alpha channel on the destination is unchanged, but the image colors are overlaid by any non-transparent parts of the source image.

If the background image is fully opaque (no transparency), this operation will act exactly like a normal 'Over' composition. It only differs when the background contains transparency, which also clips the overlay.

What makes this useful for is for overlaying light and dark lighting effects as the overlay is automatically clipped to the same shape as the destination image.

For example say we have a red circle while want to add a highlight on it as if it is a 3 dimension ball. Well we can create the circle, and the highlight then overlay the highlight using 'ATop' to limit it the the circle itself.

  convert -size 70x70 xc:none \
            -fill red -draw 'circle 35,35 10,30'  red_circle.png

  convert -size 70x70 xc:none -draw 'circle 35,35 35,20' \
          -negate  -channel A -blur 0x8    red_highlight.png

  composite -compose ATop -geometry -13-17 red_highlight.png \
            red_circle.png red_ball.png
[IM Output] [IM Output] [IM Output]

This is different to the 'In' or the 'Dst_In' compose methods, as both images can contain a transparent shape. Only the parts which are 'above' the destination or background image is overlaid.

The creation of the white highlight was done using a "-negate" to ensure the whole image is actually white. This was used to avoid a major bug in the "-blur" operator, which has now been fixed in IM version 6.2.4. See Blur with Transparency Bug for more details of this old bug.

In a similar way we can continue this to add a fuzzy shadow to our ball, clipped to the boundaries of the ball itself, so that it looks even more 3-D like. I'll leave it as an exercise to the reader to figure out how the following command achieves this result.

  convert  moon_crescent.png -fx 0 -channel A -blur 0x7 \
           red_ball.png  +swap -compose ATop -composite  red_ball2.png
[IM Output]

With a little more care with preparatory images the above image can be made even more realistic, but for showing the usage of this compose method, it isn't bad.

Further examples of using 'ATop' compose method, can be seen in Generating 3-D Logos.

Clear (Clear the Background. Ignore overlay image)

This is an unusual compose method that essentially ignores the overlay image completely, and just clears the background image.

This makes it an ideal method of creating a transparent or black canvas, the same size as the background image, for complex projects. As the overlay image is not used, a single pixel "null:" image is a good overlay to use.

  composite -compose Clear null:  rose: -matte compose_clear.png
[IM Output]
Of course, the "-matte" operator (while not needed in this case) ensures the background has a matte/alpha channel to it to clear the destination image to.

If you specifically turn off the matte channel you get a black canvas, due to the mathematics involved.

  composite -compose Clear null:  rose: +matte compose_black.png
[IM Output]

Xor (Clear the area shared)

This is a very strange and little used composition method. What it does is to overlay the two images together, but then clear the area that overlapped back to transparency.

  convert -size 60x60 \
          \( xc:none -fill blue   -draw 'circle 21,39 24,57' \) \
          \( xc:none -fill red    -draw 'circle 39,39 36,57' \) \
          -compose Xor   -composite   compose_xor_2.png
[IM Output]

Things get even more interesting when a third image is xored on the above results.

  convert -size 60x60  compose_xor_2.png  \
          \( xc:none -fill green  -draw 'circle 30,21 30,3'  \) \
          -compose Xor   -composite   compose_xor_3.png
[IM Output]

As you can see, overlapping multiple images, can produce some wonderful effects, where any even number of images overlapping will be clear, while and odd number of overlay will result in the last image overlaid showing though.

FUTURE:  Xor effects on semi-transparent images get extremely weird.
Explore this and create a example demonstrating this weirdness.


Mathematical Compose Methods

This group of compose methods lets you perform mathematics with the images. While this may not seem very useful. But for low level manipulation of images they allow you to do things which you would not normally think of.

The first thing to note is that they are rarely used with images involving transparency, as they can have very unusual consequences. But with fully-opaque images, and especially where one or both images are a gray scale, these methods come into their own.

Multiply (make white transparent for diagrams/text)

Is one of the more useful but under-rated compose method. A pure white overlay will result in no change to a fully opaque background, while darker colors darken the destination image by the same amount.

Note that it will only however only darken the background, never brighten it.


  convert label:Rose label_white.gif
  composite -compose Multiply -gravity center \
            label_white.gif  rose:  compose_multiply.gif
[IM Output]
[IM Output]

This method works very well in a lot of situations, but generally only if one (either) image is basically black (or greyscale) lines on a mostly white background. If both images contains regions of color, then you may get unusual results.

In other words. this technique is perfect for overlaying line drawings, diagrams or images of text, on a image with white, or very light colored background.

For example I needed to add some diagrams to a page of text which was to be included into a ray-traced image. You can see this page, in the images on my Studies in Polyhedra.

Given two grey scale image masks, multiply is also a good way to erase parts of an image to black based on some mask. It does this linearly, so the mask can be a greyscale image rather than a purely Boolean on/off image.

Multiplying an image with itself is actually a useful technique to produce a squared darkening of an image, so bright colors remain prominent, but other colors become darker. This is used for example for a Star Generator, to produce a more realistic distribution of star intensities. Or it can be used on satellite cloud images before overlaying the result on a geographical map.

Screen (make black transparent for diagrams/text)

This is almost exactly like 'Multiply' except both input images are negated before the compose, and the final result is also then negated again to return the image to normal.

In other words it will overlay white on black text and diagrams onto an image or other background. It will only brighten an image, never darken it, and a pure black overlay will produce no change to a fully opaque background image.

  convert -background black -fill white label:Rose label_black.gif
  composite -compose Screen -gravity center \
            label_black.gif  rose:  compose_screen.gif
[IM Output]
[IM Output]

Bumpmap (greyscale multiply)

The 'Bumpmap' method is essentially exactly the same as 'Multiply', except that the source image is converted into grey-scale before being overlaid. In other words it will darken the image anywhere the source image is dark.

  composite -compose Bumpmap -gravity SouthEast -geometry +3+3 \
            hand_point.gif   cyclops.gif   -matte   compose_bumpmap.gif
[IM Output]
Its original purpose is to add texture (like a paper or fabric texture) to images. However as it can only darken images, it is not as useful as the 'Overlay' method which can both lighten and darken images (see below).

Divide (removing shading effects)

Basically the destination image is divided from the source image. However as color values are thought of as always between 0.0 (black) and 1.0 (white) this will in fact brighten the source image by the amount of black that appears within the source image.

Remember that the order of the images is as if you are reading the "composite" command line. that is Src OP Destination. So this "-matte" method acts as if you are reading
" Source Divided_By Destination "
making the destination image the divisor.

The source will always be brightened, by the amount of darkness that exists in the destination image. If the destination image is pure white, the source image will be left unchanged (assuming both images are the same size).

Now this may sound weird but it is actually a very useful operation. It means you can effectively remove dark patches from an image caused either by lens vignetting, bad scanning, or tinting caused by age.

For example here I have a image of some hand written text which was very roughly scanned (using a digital camera without flash, then reduced in size to remove digital noise). The paper had faded edges, and as a flash was not used the background is very dark, and has shading effects because the paper wasn't completely flat.

[IM Output]

Just normalizing the image to enhance the contrast of the image doesn't help to remove the edge coloring or other shading effects.

  convert text_scan.png -normalize text_scan_norm.png
[IM Output]

However by blurring the image to remove the lines, and then dividing that from the original, we effectively remove all the gross shading effects from the image.

  convert text_scan.png \( +clone -blur 0x20 \) +swap \
          -compose divide -composite  text_scan_divide.png
[IM Output]

As you can see it is a lot clearer that just normalizing it. Though some of the anti-aliasing was also removed. Some extra adjustments could have been made to the divisor image, but it is very close to what was wanted.

This technique is also used in the Removal of Vignette from images.

This operator could be combined with a background gradient generator (still to be added to IM) to generate better 'divisor' images, than just bluring thin lines.

Plus (Add colors together to form a blend)

Basically adds the colors of the overlay to to the background. In essence causing the two images to blend together equally.

Note however that if the added colors overflow the color limits, the color will be capped (or clamped) and the result not as you would expect.


  convert -size 60x60 xc:none -fill red \
          -draw 'circle 30,21 30,3'   compose_R.png
  convert -size 60x60 xc:none -fill 'lime' \
          -draw 'circle 21,39 24,57'  compose_G.png
  convert -size 60x60 xc:none -fill blue \
          -draw 'circle 39,39 36,57'  compose_B.png
  composite -compose plus  compose_R.png compose_G.png  compose_plus_RG.png
  composite -compose plus  compose_R.png compose_B.png  compose_plus_RB.png
  composite -compose plus  compose_G.png compose_B.png  compose_plus_GB.png
  composite -compose plus  compose_R.png compose_plus_GB.png \
            compose_plus_RGB.png
[IM Output] [IM Output] [IM Output]
[IM Output] [IM Output] [IM Output] [IM Output]

Due to IM's compliance to the SVG standard, 'green' is not a true green color (like it is in X11 color names) but a dark or half bright green. As such it was not used in the above as you need a true green to generate a white color. A true RGB green can be specified with the color 'lime' as we have done in the above example.

'Plus' is sometimes used to add white text to an image. This is NOT correct usage, and may result in some anti-alias inconsistencies. The better way is to use the 'Screen' as I showed above. This uses a multiplication rather than a additive method of increasing brightness.

This composition is also the basis for Blending Images.

In 'Photoshop 7' this composition is known as 'Linear Dodge', though it also has it called 'Add' but with scaling for 'blending' purposes.

Add (modulo-plus)

The 'Add' composition is exactly the same as 'Plus' except that when the resulting color goes beyond white, it is wrapped around (modulus) back to black.

For example if we add two grayscale gradients, the brightest colors will be wrapped to form a second gradient.

  convert -size 60x60 gradient:    gradient.png
  composite  gradient.png gradient.png -compose Add  compose_add.png
[IM Output]

This Modulus-Add type of composition can be used to produce some very interesting gradients such as.

  composite gradient.png \( gradient.png -rotate -90 \) \
            -compose Add   gradient_diagonal.png
[IM Output]

And multiple modulus additions can make venetian blind type repeated gradients.

  convert gradient.png \( +clone +clone +clone +clone \) \
          -background gray50 -compose Add -flatten  gradient_venetian.png
[IM Output]

Which produced a gradient which was repeated 5 times. Note how I used a background of 'gray50' to effectively 'roll' the gradient half a modulus cycle forward.

Minus

This is an mathematical oddity in the math compose methods, The result is an image in which the colors of one image is subtracted from the other. However the opaqueness of the pixels are also subtracted (if present), as such you will not see the result unless you also turn off the images matte channel.

For example lets subtract one circle from the final result of the 'Plus' operation above.

  composite -compose minus  compose_plus_RGB.png  compose_R.png \
            compose_minus.png
[IM Output]

Note that while the above result looks the similar to a 'Out' or a 'Dst_Out' method, in that it will 'erase' parts of an image, the colors are also effected. As such do not use it to erase parts of images.

We can see that colors were also effected, removing the red circle completely from the source image, if we make all the pixels full-opaque. That is this is a color and alpha channel subtraction.

  composite -compose minus  compose_plus_RGB.png  compose_R.png \
            +matte compose_minus2.png
[IM Output]

In 'Photoshop 7' a 'Linear Burn' is the same, but with the source image negated first.

Subtract (Modulus Minus)

The 'Subtract' operator is basically exactly the same as 'Minus' except that it is a modulus subtraction, so that subtrating 'white' from 'gray' will result in a 'gray', and not black, as the values wrap back around.

As side effect of this subtracting the transparency channel will also have wierd effects, resulting in different parts of the image being made transparent and opaque.

  composite -compose subtract  compose_plus_RGB.png  compose_R.png \
            compose_subtract.png
[IM Output]

As with all mathematical operators it is probably best to handle transparency separatally to the color channels, with care so that you get the results you are actually expecting.

Difference (image compare, and selective negate)

The resulting image is the absolute difference in the color values. The difference between the colors 'black' and 'white' difference will produce a maximum result of white. While any colors which are the same will produce black. Basically a simple form of image comparator.

The more common use of this composite method is for comparing two images of the same size to see how they differ, and even produce a percentage of that difference.

This operator is typically used to generate Comparison Difference Images.

The operator also can be used to selectively negate an image. Overlaying black has no effect on the background, while overlaying white, negates the colors in the image at that location.

For example lets negate half the rose image.

  convert -size 2x1 pattern:gray50  -scale 70x50\! black_n_white.gif
  composite black_n_white.gif   rose: \
            -gravity center -compose difference   compose_negate.gif
[IM Output]
[IM Output]

Exclusion (image difference excluding greys)

This is sort of an averaged difference. White on white will produce black (no difference), the same with black on black. However grey on grey will produce a grey result. White and black naturally still produce white (maximum difference).

Another way of looking at this is that bright image regions cause inversion to the respective other layer, very dark regions change nothing at all. In that this compose method is the same as 'Difference'.

What is if for? I have no idea... Anyone have a good example?

One use thought of by from Joe Fry, was as a way to merge multiple difference images together, That way that the final image shows the changes of all the images.

Eg: Given A, B, C, then use difference for A,B and B,C then use exclusion on those results so that you get one image showing how all three images changed.


Color Manipulation Methods

These methods modify the colors of image in highly complex ways. Often involving some type of choice, or if-else expression in the selection of what color value, or even mathematical expression is used to set the final color in the resulting image.

Overlay (add color to a greyscale object)

This compose method is very unusual in that it has been designed to both 'Multiply' (darken) and 'Screen' (lighten) at the same time. Which method chosen is selected by a 50% cut-over.

The result is a special compose method which preserves any pure white and black (highlights) that is present in the background, while tinting the mid-tone greys by the colors found in the overlay image, with the overlaid color image.

That is given a grey scale image as a background, then it will color that background using the source image, preserving any 3 dimensional shadow and highlighting effects that is present.

For example...

  composite rose:    \( -size 46x70 gradient: -rotate -90 \) \
            -compose Overlay   compose_overlay_rose.jpg
[IM Output]

You can probably see this more clearly using the gradient example from Tables of Compose Methods, which I'll reproduce here...

  convert -size 64x64 gradient:yellow-blue gradient_yell-blue.jpg
  convert -size 64x64 gradient: -rotate -90 gradient_grey.jpg

  composite gradient_yell-blue.jpg  gradient_grey.jpg \
            -compose Overlay   compose_overlay_gradients.jpg
[IM Output]  + [IM Output] ==> [IM Output]

This method, unlike 'Multiply' or 'Screen', is not associative.

  composite gradient_grey.jpg  gradient_yell-blue.jpg \
              -compose Overlay    compose_overlay_gradients2.jpg
[IM Output]  + [IM Output] ==> [IM Output]

However just swapping images like this is actually what the 'Hard_Light' composition method, does. The only difference is which image (the color tints, or the greyscale) that was used as the destination or background image, and thus the final image size.

This tinting or adding color to mid-tine greys, makes this method perfect for adding color to the output of the "-shade" operator. However you need to be very careful to generate a perfect mid-tone grey from that operator. See Shade Overlay Highlights for details.

For example given a circle, we can shade it to produce 3 dimensional effects, then tint the result with the original image, to restore the original colors.

  convert -size 64x64  xc:dodgerblue \
          -draw 'fill skyblue   circle 32,32 7,27'  circle_blue.jpg
  convert circle_blue.jpg  -shade 120x30  circle_shaded.jpg
  composite circle_blue.jpg  circle_shaded.jpg \
            -compose Overlay    circle_shaded_tinted.jpg
[IM Output]  + [IM Output] ==> [IM Output]

For an more practical example of using the 'Overlay' method in this way, see the "levels_3d" image on the Background Generator Examples page.

The biggest problem with this compose method is that any transparency in either image will not be preserved. That is what is visible in either image will be visible in the result.

As overlays using this method generally have no transparency, it usually means you may need to do some extra steps to restore the transparency of the destination or background image. For examples of doing this see Overlay Color Tinting. Or a more practical but complex example in Better 3-D Logo Generation example.

Up until IM version 6.1.6 the 'Overlay' compose method was broken in that it would only produce pure black or white results (most likely you would only get a pure black result). Consequently IM users had little understanding of this operator. Hopefully this will now change.

Hard_Light (add texture or highlight/shadow to an image)

This is exactly the same as 'Overlay' except the source and destination images are swapped.

So while 'Overlay' can be thought to added color to gray-scale shaded object, 'Hard_Light' can be thought of as adding highlights to a colored image. The result is basically the same, just with the source and destination images swapped.

The typical use of this composition method is to add 'texture' to an existing image.

For example, here I adjust the colors of the "rose:" built-in using a "granite:" texture (after centering its histogram and muting its effects)...

  convert rose: \
          \( granite: -blur 0x.5 -normalize -fill gray50 -colorize 70% \) \
          -compose hardlight -composite compose_hardlight.jpg
[IM Output]

Note that overlaying pure black or pure white will result in the destination image being pure black or white, in those areas. As such you may like to remove the contrast of a overlaid texture (such as the use of "-colorize" in the above) to mute the effects of such extreme colors.

Of course if you want to add both extreme highlights (like a reflected light) or dark shadow effects to an existing image, using the one operation, then this operator is ideal for your purposes.

For another better example of this see Overlying a Texture in the Photo Cookbook.

Soft_Light (softer highlighting of an image)

The 'Soft_Light' compose method will also add highlights and shadows to an existing color image. However the colors of the destination image is modified as a change in way resembling a 'gamma function', producing a softer contrast to the colors.

For example here is the a 'Soft_Light' texture mapping.

  convert rose:   \( granite: -blur 0x.5 -normalize \) \
          -compose softlight -composite compose_softlight.jpg
[IM Output]

As you can see, even with a normalized "granite:" image, the color changes are softer and less intensive than that of 'Hard_Light', or even 'Overlay'. However you also will never get any pure black shadow or pure white highlights, added to the resulting image.

Basically 'Soft_Light' has a softer texturing effect, which preserves the original coloring of the image.

Here I show the color change limits achieved as a result of using pure black, white and gray color overlays.

  convert -size 46x70 gradient: -rotate 90 \
          -ordered-dither threshold,3   grayscale_3.gif
  composite grayscale_3.gif  rose: -compose softlight  compose_softlight_2.gif
[IM Output]
[IM Output]

ERROR: note that the black did not darken the image. This is a known bug but the compose method has proven to complex, to debug easily. -- Can you help?

Lighten and Darken (select color based on brightness)

Compare the source and destination image color values and take the respective lighter or darker value.

A typical use of 'Lighten' is to compose a blurred version of the original image, so that the 'highlights' in the original image get a soft glow about them, while dark parts are not similarly blurred. For one example see Polar Distortion Tricks.

Example needed...

Unfortunately these operators work on each RGB channel separately, and as such this could result in some color distortion.

To avoid this color distortion problem, you should probably select the lighter/darker color based on overall color intensity.

Specifically, create a gray-scale image of the two images and 'Lighten' or 'Darken' them as appropriate, You can now use a 'Change_Mask' to compare that result with one of the gray-scale images used for this comparison. This 'mask' then be used select which color comes from which of the original colored images.

Note that "Gimp", "Photoshop", "PhotoLine", "Paint Shop Pro", all compare channels directly just like IM. However "Photo-Paint" compares the images by color intensity, and uses that to decide which image to select colors from, thus preserving color integrity. On the other hand "Picture Publisher", and "PhotoImpact" use the color lightness (as defined by the HSL colorspace) for the comparison.

FUTURE: Suggested technique from Magick Mailing list...

IM Forum User 'form' reports that he
applies the 'Darken' operator over a 100
snapshots of a tv-series.  This final image can then be used to identify the
TV-logo that was visible in all the snapshots, allowing you to de-logo the
entire video sequence.

To thicken the lines of a vector graphics image, you would want to composite
the image many times slightly offset with the "-compose darken" operator. That
is, you would composite the source nine different times: at -1,-1; -1,0; -1,1;
0,-1; 0,0; 0,1; 1,-1; 1,0; and 1,1.

Gabe Schaffer <gabe.com@gmail.com>

If colored images are used, you could just thicken the lines in just the
'luminance' channel of HSL colorspace version of the original image.
Again this has NOT been tried or verified. -- Anthony

Color_Dodge (protect from light exposure)

This composition method uses the source overlay image as a mask, that sort of protects the background image from "light exposure" over long periods of time. The brighter the overlay is, the more the background is lightened.

Think of it as a placing some shape on top of an image for a very very long time.

A pure black overlay covers the background from all changes, while a pure white overlay will make all the background colors white, except the pure black.

Example wanted

Color_Burn (protect from light exposure)

This is the reverse of 'Color_Dodge', equivalent to inverting all the input and output images. The result is that that the background image is darkened by a dark masking image. White will produce no darkening.

Linear_Light (???)

Brightens the destination color to reflect the source color. Painting with black produces no change.

What does this mean, I am not sure. Anyone have any example.


Channel Coping Methods

These image composition methods are designed to transfer image channel information from one image to another. It however makes some assumptions about the image from which the 'channel' is being copied. I suggest you read the sections Image Color Space and Color Channel Operators to understand how IM represents image color channels in memory before using them.

The most useful channel coping composition methods is 'Copy_Opacity' (see below). It is about the only way to simply replace (or add) just the alpha channel to an existing image using a separate greyscale mask image.

Copy_Opacity (Set transparency from gray-scale mask)

The original purpose of the 'Copy_Opacity' operator was to copy the transparency channel of the source image into the destination image, to set that images transparent parts. But this more commonly done using Duff-Porter operators, which are specifically designed for compositing images with alpha transparencies.

For example if the destination is fully-opaque, which is typical for what this operator is used for, then either 'Dst_In', or even 'Dst_ATop' operators will achieve the same result.

So why is the 'Copy_Opacity' operator so important, when their are alternatives?

The answer is how this operator handles a special case. When the overlay (source) image has no matte (alpha or matte) channel, then this operator will treat it as a simple grey-scale image mask. That is it will replace any existing alpha channel (actually a negated matte channel) of the image with the greyscale image given.

In summary anything that is black in the gray-scale image will be thought to be fully transparent, and white fully-opaque.

This is so important, and so commonly used, I will make it a rule of thumb...
Images using this operator should NOT contain any alpha channel.

You can ensure that the images do not have any alpha channel by always using a "-alpha Off" method, or using the older less clear plus form of the "+matte" operator, on both input images before compositing using 'Copy_Opacity'.

For example, lets make a grey scale moon image (which is easy to draw), then use it as a mask for a blue plasma gradient, to produce a nice mottled looking blue moon.

  convert -size 70x70 xc:black -fill white -draw 'circle 35,35 35,5' \
                               -fill black -draw 'circle 28,30 35,5' \
          moon_mask.gif
  convert -size 70x70 plasma:white-dodgerblue  moon_mask.gif \
          -alpha Off  -compose Copy_Opacity   -composite  moon_gradient.png
[IM Output]
[IM Output]

If your images has transparent components that that you do need to preserve, then this is not the Alpha Composition operator you need. More than likely you want to use 'Dst_In' instead, to subtract the transparent areas of the source overlay from your destination (in which case both images need a "-matte" channel added.

This operator is explained in even more detail in Using a Mask Image with Fonts and in Channels - Erasing Parts of an Image. It is also used in many other examples throughout these pages.

Copy_Red, Copy_Green, Copy_Blue

Copy the given color channel of the source image into the destination image.

As a gray-scale RGB image has the same data in all three color channels, these methods can be thought of as coping a greyscale channel image to replace the specified channel in the destination image. Much as the 'Copy_Opacity' (see above) can replace the 'alpha channel' of an image with a greyscale image.

These channel coping methods are rarely used today thanks to the use of various Color Channel Operators to separate out and re-combine color channels via grayscale channel images.

Note that merging various grayscale gradient patterns, very interesting color images can be generated using these operators. For some examples look at the color gradient combinations in raw compose operator tables.

Copy_Cyan, Copy_Magenta, Copy_Yellow

These are synonyms for the same methods in the previous section.

Basically as the 'Red' Channel in RGB images is also used as the 'Cyan' channel in the CYMK images. The same for 'Green' and 'Magenta' channels, and the 'Blue' and 'Yellow' channels.

Because of this copying the 'Cyan' channel is the same as coping a 'Red' channel. What type of channel the image data is representing depends on the Color Space of the image in memory.

Copy_Black

This also just copies the 'Black' channel from source to the destination, if it exists. However that channel only exists for CMYK images.

If the 'Black' channel does not exist this does nothing, which is probably a bug. What it probably should do is copy a grey-scale 'Black' channel image, to the black channel of the destination image (assuming that it exists).

Under Construction

Hue (copy the hue of a RGB image to destination)

This operator copies the hue H of the source image to replace the hue of the destination image, leaving the backgrounds saturation S and luminance L (OR is it luminance Y?) unchanged.

This assumes both images are in RGB colorspace image. I have not tested this with a non-RGB colorspace image.

Saturate (copy the saturation of a RGB image to destination)

Copies the Saturation S from source to destination, (assuming the image is a RGB colorspace image) leaving the color hue H and luminance L (OR is it luminance Y?) unchanged.

This assumes both images are in RGB colorspace image. I have not tested this with a non-RGB colorspace image.

Luminize (copy the luminance of a RGB image to destination)

Copies the luminance L (OR is it luminance Y?) from source to destination, leaving the color hue H and saturation S unchanged.

This assumes both images are stored using RGB colorspace. I have not tested this with a non-RGB colorspace image.

Colorize (copy the color hue and saturation to destination)

Copies the hue H and saturation S from source to destination, leaving the color luminance L (OR is it luminance Y?) unchanged.

This assumes both images are stored using RGB colorspace. I have not tested this with a non-RGB colorspace image.


Special Composition Methods

There are also a few special purpose composition methods that have been added for one reason or another. Typically because of some need by internal programming within IM.

Change_Mask (Make similar pixels transparent)

This is an unusual method that will only make specific pixels in the destination image full-transparent. That is the pixels in the destination image that matches the given source image, according to the current Fuzz Factor setting.

This can be used for example to re-add transparency to an image that was overlaid onto a complex (but very different) background. For example, this was exactly the problem presented by Jesper Eije...

  convert bgnd_overlaid.gif   bgnd.gif  \
          -compose ChangeMask  -composite  bgnd_removed.png
[IM Output] [IM Output] ==> [IM Output]

Easy.

Note that as JPEG image colors often have slight color changes due to the lossy nature of the file format, a small Fuzz Factor setting will be needed to match very similar colors

Also if the changed part of the image happens to actually match the background color or pattern, you will get 'holes' in the resulting image or mask, as such caution is always advised when using this with images that are very similar.

By swapping the two images, so the background image is given as the destination image, you can extract the parts of the background that was replaced or modified by the overlaid image.

  convert bgnd.gif  bgnd_overlaid.gif \
          -compose ChangeMask  -composite  bgnd_replaced.png
[IM Output]

Note that the result is only a transparent shape mask of the original background image where similar pixels are just cleared to transparency. It will makes no attempt to generate partial transparencies, or change colors, and as such could leave a 'halo' of slightly different background colors around the image where the original overlay and the background color merged together.

As this composition method only 'clears' pixels to full-transparency, a matte channel is always added to the resulting image. The color of the destination image is not however cleared as it is in most other Alpha Composition methods. As such you can negate the alpha channel, to get the unchanged colors.

  convert bgnd_overlaid.gif   bgnd.gif -compose ChangeMask  -composite \
          -channel A -negate      bgnd_unchanged.png
[IM Output]

In this case the order of the input images does not matter, unless a large Fuzz Factor was set. Remember the colors will come from the destination image, with the difference between the images defining the resulting shape mask.

For more information on these problems see Re-adding Transparency to an Image.

Also the result is often limited in its direct usefulness. It can however be used to generate bitmap masks of the changes between the images (hence its name).

The 'ChangeMask' composition method was added to IM v6.3.4, to provide a method for Optimizing Transparency for GIF Animations. No other existing composition method provided the needed requirements, without combining 3 or more composition methods. Specifically the Boolean (or fuzz factor thresholding) requirement needed for transparency optimization.

The result however is so useful in its own right that it was made available for direct use by users, and so defined the name of this rather complex composition method.

Other Special Compositions

There are a few other special composition methods, though these generally require extra input arguments and so are not directly usable via the command line "-compose" method.

These include the Dissolve, Blend, Watermark, and Displace "composite" command methods, which are shown below, as well as composition methods for Thresholding Images.


Dissolve One Image Over Another

What the "-dissolve" operator does is provide a controlled 'Over' compose method. It adjusts the transparency of the overlay image, before it is overlaid on top of the the background, according to the percentages given.


    composite -dissolve {percent} overlay  bgnd  result


composite -dissolve {src_percent}x{dst_percent} overlay bgnd result
Providing two arguments (new to IM v6), allows you to exactly define how much of both the overlay and the background image is dissolved before the images are overlaid.

If only a single percentage argument is given, the overlay is dissolved to leave that amount, (0 percent is completely transparent, 100 source is unchanged) before being overlaid (using the 'Over' compose method) onto the background. Furthermore, if you continue past a value of 100% (also new for IM v6), it will then start to dissolve away the background image.

In other words, in the 0 to 100 percent range, the background image is not dissolved at all (with a dst_percent of 100%), while in the 100 to 200 range, the overlay is left as is, (src_percent set to 100%), and the background is dissolved, so it is completely gone when the argument reaches a value of 200%).

Here is a table of results with various dissolve arguments...

  composite -dissolve {argument}  -gravity South \
            star.gif   dragon_sm.gif -matte   {result}
[IM Output] [IM Output]
[IM Output]

As you can see "-dissolve" will first slow dissolve a new image over the background, then slowly dissolve away the original background image. Which is very suitable for images containing transparent areas. You can also control of the dissolve of the background, so while one image dissolves in on top, you can arrange for the background to dissolve away underneath.

This is very good, but is not really suitable for a proper slide show style of dissolve between images. As one image is overlaid the images are not merged together equally, and swapping the image arguments (and corresponding dissolve arguments) will produce different results.

For the 100% to 200% range, or the second percentage argument to work, the background image must contain a alpha channel. This is especially important for JPEG images which do not contain any transparencies by default.

As such it is probably a good idea to tell IM to give all image arguments an alpha or transparency channel using the "-matte" operator, before the "-dissolve" is applied to overlay the images.

The following are special cases for specific "-dissolve" arguments...
0x0 Both images are dissolved to full transparency.
Equivalent to a 'Clear' compose method.
0 Just the background image unchanged.
Equivalent to a 'Dst' compose method.
200 The overlay image on a blank canvas.
Equivalent to a 'Src' compose method.
100 A normal overlay of the overlay image onto the background.
Equivalent to a 'Over' compose method.

The "-dissolve" compositing method is commonly used as an alternative way of Watermarking with Images, particularly with color watermarks.


Blend Two Images Together

The "-blend" compositing method provides what the "-dissolve" compositing method was originally intended to provide, before it was hijacked for other more basic operations.

Where as "-dissolve" method overlays images on top of each other, "-blend" method merges images together such that both images are treated equally (being just added together), according to the percentage arguments given. As such you can achieve the same result (except in final image size) by swapping the percentages and the image arguments. This is not the case with "-dissolve" method.

In otherwords, "-blend" is associative (arguments are swappable), while "-dissolve" is not.


    composite -blend {percent} overlay  bgnd  result


composite -blend {src_percent}x{dst_percent} overlay bgnd result
If only a single percentage value is given, it is used as the dissolve percentage for the overlay, while the background image is dissolved by the exact opposite amount. That is a "-blend 30" will dissolve the overlay by 30%, and the background by 70% before the two images are added together. thus it is equivalent to "-blend 30x70".

By giving two percentages you can control exactly how much of the two images are to be merged together.

Here is a table of results with various dissolve arguments...

  composite -blend {argument}  -gravity South \
            compose_R.png compose_plus_GB.png  -matte  {result}
[IM Output] [IM Output]
[IM Output]

From the results you can see that "-blend" will let you slowly replace one image with another equally without overlay.

If you overlay the result of a blend on a black canvas, say the size of your display, you can very easily make a very respectable slide show program, in which one image slowly dissolves into another.

You can also see that because the images are added together (internally it uses the 'Plus' compose operator), you can easily overload the color channel maximums, producing some artifacts. A 100% white color in particular will ensure nothing of the other image will be visible as all colors channels are already at their maximum.

It is thus recommended that in most situations you would only need to use the single argument version of blend.

As with "-dissolve", the background image must contain a alpha channel for "-blend" to work correctly, to dissolve the background image.

As such it is probably a good idea to tell IM to give all image arguments an alpha or transparency channel using the "-matte" operator, before the "-blend" is applied to overlay the images.

The following are special cases for specific "-blend" arguments...
0x0 Both images are dissolved to full transparency.
Equivalent to a 'Clear' compose method.
0 Just the background image unchanged.
Equivalent to a 'Dst' compose method.
100 The overlay image on a blank canvas.
Equivalent to a 'Src' compose method.
100x100 Just the two images added together.
Equivalent to a 'Plus' compose method.
50 Average of both images. This is equivalent to the "-average" image sequence operator, but only with two images. (See also Averaging Images)

Using Blend to Modify a Single Image

Blend is basically an 'interpolation function' allowing you combine two images together in a linear way. That is 30% of the destination and 70% of the source.

It can however do more than just combining two images, but can also be used to brighten, set contrast, saturation, colorize and even sharpen a specific image. This use of blend is discussed further in a document found by BugBear <bugbear@papermule.co.uk> on the WWW, Image Processing by Interpolation and Extrapolation a copy of the one on Grafica Obscura (the page is not directly linkable from external source). This document is recommended for further reading on using this function.

Extrapolation ability was added to the "-blend" operator in IM v6.3.5-10.

For example here we can make the rose image brighter or darker by blending it with a pure black image of the same size.

  composite  -blend   0  rose: -size 70x46 xc:black -matte blend_dark_0.jpg
  composite  -blend  50  rose: -size 70x46 xc:black -matte blend_dark_50.jpg
  composite  -blend 100  rose: -size 70x46 xc:black -matte blend_dark_100.jpg
  composite  -blend 150  rose: -size 70x46 xc:black -matte blend_dark_150.jpg
  composite  -blend 200  rose: -size 70x46 xc:black -matte blend_dark_200.jpg
[IM Output]
0%
[IM Output]
50%
[IM Output]
100%
[IM Output]
150%
[IM Output]
200%

As in any of these extrapolated blend examples, 0% is the 'destination' image the "-blend" operation is being applied against, while 100% is the original 'source' image, unchanged.

Similarly using a pure gray image you can adjust any images contrast.

  composite -blend   0 rose: -size 70x46 xc:'#808080' -matte blend_gray_0.jpg
  composite -blend  50 rose: -size 70x46 xc:'#808080' -matte blend_gray_50.jpg
  composite -blend  80 rose: -size 70x46 xc:'#808080' -matte blend_gray_80.jpg
  composite -blend 100 rose: -size 70x46 xc:'#808080' -matte blend_gray_100.jpg
  composite -blend 120 rose: -size 70x46 xc:'#808080' -matte blend_gray_120.jpg
  composite -blend 150 rose: -size 70x46 xc:'#808080' -matte blend_gray_150.jpg
[IM Output]
0%
[IM Output]
50%
[IM Output]
80%
[IM Output]
100%
[IM Output]
120%
[IM Output]
150%

Here I apply the rose image against a blurred version, producing a 'unsharp' operation (sharpening an image by extrapolating from its blurred form).

  convert rose: -blur 0x5  rose_blurred.png
  composite  -blend   0  rose: rose_blurred.png -matte blend_blur_0.jpg
  composite  -blend  50  rose: rose_blurred.png -matte blend_blur_50.jpg
  composite  -blend 100  rose: rose_blurred.png -matte blend_blur_100.jpg
  composite  -blend 150  rose: rose_blurred.png -matte blend_blur_150.jpg
  composite  -blend 200  rose: rose_blurred.png -matte blend_blur_200.jpg
[IM Output]
0%
[IM Output]
50%
[IM Output]
100%
[IM Output]
150%
[IM Output]
200%

The ImageMagick composite "-blend" operator can also be used to control the general transparency of an image, as you can specify a separate 'weighting' of the blend for each of the two images. If the two numbers adds up to less than 100% then the result is a transparency addition.

You can think of this as blending an image with a fully transparent image.

For example lets create a semi-transparent rose image. As the 'source' image in this case will have no contribution to the result, you can just use a simple single pixel "null:" image.

  composite  -blend 0x50  null:  rose: -matte    blend_semitrans.png
[IM Output]

This however is not very convenient as the "-blend" operator is currently only available in the "composite".

Remember that we do need to ensure a matte or alpha channel is present any input images for any of the above operations to work correctly.


Watermark Option Usage

The "-watermark" compose operator is meant to be used to purposefully dis-color images for copyright purposes.


    composite -watermark {brightness} overlay  bgnd  result
However the operation seems to be designed with purely black and white images in mind, and will not handle color or grey scale images. Any such images given will be automatically translated to a a pure black and white image, usually with ugly results.

White parts lighten the image by the watermark percentage, while black parts darken the image by a similar amount. A pure black or white watermark image is NOT a good idea.

Here is a table of results with various arguments...

  composite -watermark {argument}  -gravity South \
            sphinx.gif  cyclops.gif   {result}
[IM Output] [IM Output]
[IM Output]
A good value for lightly watermarking images is a brightness of about about 15 to 30 percent.

For other watermarking techniques see, Watermarking with Images.

Using a Compose Mask to Limit the Composed Area

The "composite" command and "-composite" operator will also take a third masking image which will limit the area effected by the "-compose" method.

For example given two images, and a mask image you can overlay part of the source image onto the background image, as defined by that mask. Please note however that the background image still defines the final size of resulting image.

  composite tile_water.jpg   tile_aqua.jpg  moon_mask.gif   mask_over.jpg
[IM Output] [IM Output] [IM Output] ==> [IM Output]

The mask image is considered to be part of the destination image, defining what parts of the destination can be modified by the result of the alpha composition. That is, the white parts can be modified, while black parts (and parts outside the mask image) are left as is.

Unfortunately while "-gravity" and "-geometry" settings will let you re-position the source image, the mask image is NOT re-positioned, and remains aligned with the destination image.

So lets just be clear about this...
The mask is aligned to the original background image.
Any gravity/geometry setting is not applied to it.

That means that if I want to re-position the mask, I need to enlarge the mask image with the appropriate amount of black rows and columns. See Splice, adding rows and columns of pixels. For example here is a "convert" version (source and background image arguments are swapped), with the mask image adjusted so as to move it relative to the background image.

  convert tile_aqua.jpg   tile_water.jpg \
          \( -background black -splice 10x25+0+0 moon_mask.gif \) \
          -composite   mask_offset.jpg
[IM Output]

Remember that the the "convert" commands "-composite" operator, specifies the destination or background image is given first.

The result is like giving the mask a offset of '+10+25' but with neither the source or background images moving.

If you want to align the mask with the source image you may be better off masking the source image separately (using 'Copy_Opacity' or some other technique). Then you can position that result onto the background without alignment problems.

Here is a similar example, but this time I use a gradient mask the same size as the background image so as to blend the two images together, producing a 'shallows' effect.

  convert tile_aqua.jpg  tile_water.jpg  -size 94x94  gradient: \
          +matte  -composite   water_shallows.jpg
[IM Output]

This provides a simple way of creating a overlapped version of two images. See Overlapping Photos.

Compose Mask and Transparency

You may think from the above that using a three image composite mask is very much like using the 'Copy_Opacity' to set a transparency of the overlay image, then composite that over your background. And for images without any transparency (such as the above) you would be right. But that is not the whole story.

First the mask is tied into the destination image, rather than the source image, so is not offset or gravity effected.

Also unlike 'Copy_Opacity' the mask image is treated as purely a gray-scale image. IM will ignore any and all alpha or matte transparency the mask may have. As such you do not need to worry about the use of a "+matte" operator to junk any alpha/matte channel in the mask image.

And finally the mask works by limiting the area which was modified, which is very different to just masking the source image.

For a simple 'Over' composition, this basically the same as if the mask is multiplied with any transparency in the source image, to limit what is overlaid. As such for completely opaque source images you can think of it as the transparency for the source image.

For example here I overlay are two circles as normal, then I overlay them with a mask limiting the area in which the destination image is changed.

  convert -size 60x60 xc:none -fill red  -draw 'circle 35,21 35,3'  m_src.png
  convert -size 60x60 xc:none -fill blue -draw 'circle 21,39 24,57' m_bgnd.png
  convert -size 60x60 xc:   -draw 'polygon 0,59 59,0, 0,0'          m_mask.png

  composite m_src.png  m_bgnd.png                   m_over.png
  composite m_src.png  m_bgnd.png   m_mask.png      m_over_masked.png
[IM Output] [IM Output] ==> [IM Output]
[IM Output] [IM Output] [IM Output] ==> [IM Output]
The result is equivalent to the red circles image being 'masked' by the given gray scale mask, which is then overlaid onto the destination image.

However with any other type of alpha composition method, you are better of just thinking of the mask as limiting the area of effect, rather than as a transparency mask of the source image.

Here I used a 'Src' method to replace the masked area in the destination with the contents of the source image.

  composite  m_src.png  m_bgnd.png  m_mask.png  -compose Src  m_src_masked.png
[IM Output]

As you can see only the area with the defined mask is actually replaced by the result of the alpha composition. If the mask was only used to modify the transparency of the source image the above would only show the source image and nothing from the background image.

FUTURE:  Using masked composition to overlay an object in front of a
backdrop, but behind some foreground object.  Also develop a animated example,
of something being obscured by foreground objects in the destination. 
The above was a major long term bug involving composite masking when you involved source or overlay images with transparency, and or attempted to use other alpha compositions other than the 'Over' compose method. See Composite Mask Bug for details.


Compose using Image Tiles

The "-tile" setting in "composite" is very different to that same setting in either "convert" or "montage". If given it means that the source image is to be tiled over the background image.

For example here we tile a 'color star' over a netscape color map image...

  composite  -tile star.gif   netscape:   tile.gif
[IM Output]

One common use of composite tiling is to tile a semi-transparent copyright or some other notice over images before publishing them on the net. For examples of this see the last examples of Watermarking with Text.

Note that the setting is essentially (at least at this time) argument-less. It is only a Boolean on and off flag to the final "composite" operation. Consequently you can not use it to tile a background image as that would mean IM can not use the background image to set the final size of the result.

However you can tile an image under another image using 'Dst_Over' instead.

  convert -background none -pointsize 36 label:'Tile Under'  png:- |\
  composite -tile bg.gif  -   -compose Dst_Over   tile_under.gif
[IM Output]

Also many of the mathematical operators are associative, meaning the source and destination can be swapped without effecting the result (except the final image size).

Currently there is no simple method of doing a tiled alpha composition in the "convert" command. This is a shame as it would make Tiling with an Image already In Memory a lot easier than it currently is.


Created: 5 January 2004
Updated: 19 January 2009
Author: Anthony Thyssen, <A.Thyssen@griffith.edu.au>
Examples Generated with: [version image]
URL: http://www.imagemagick.org/Usage/compose/