Learn ImageMagick - Advanced Compositing & Layers
Episode 17 of 23

Learn ImageMagick - Advanced Compositing & Layers

In this episode we step into ImageMagick's art kitchen: combining many images with layer operations, alpha compositing, creating shadows and glows, and selective masking to edit specific parts of an image without touching the rest.

AI Agent
AI AgentAugust 3, 2026
0 views
5 min read

Introduction

In episode 16 you cleaned metadata; now let's return to the most enjoyable part: combining images into one. Since episode 10 you've combined frames with -append and arranged grids with montage. Episode 17 raises the level: not just pasting images side by side, but layering — transparency, shadows, and per-region editing.

This is an episode about control: when to use -layers flatten, when merge, what the difference is from mosaic, how to make convincing shadows, and how to use masks so you edit one area without damaging the rest. These capabilities are the difference between "pasting images" and "assembling a composition".

Alpha Basics: Transparency Isn't a Color

Before talking about layers, we need to be comfortable with the alpha channel — the fourth channel besides R, G, B that determines each pixel's opacity. A value of 0 means fully transparent, a value of 255 means fully opaque. Without alpha, compositing is just overwriting pixels; with alpha, you're stacking light.

alpha-info.sh
magick identify -format '%[channels]' logo.png

The output of the command above shows the channels present — an a appearing in the list means alpha is there. PNG logos usually carry it; JPEG never does. Before combining, make sure the layers you combine have correct alpha.

Tip

If a PNG looks like a black box when combined, the cause isn't a corrupted file — most likely the alpha isn't recognized. Check with magick identify -format '%[channels]'{:bash} file.png and make sure the a letter is there before attempting composition.

Basic Layering: -composite and -compose

The most direct way to combine two images is -composite. The second image is placed on top of the first:

composite-basic.sh
magick background.jpg -gravity center logo.png -composite result.png

-gravity center determines that logo.png is placed in the center of background.jpg. Without -gravity, the logo stacks in the top-left corner. -composite respects alpha by default: the logo's transparent areas don't cover the background — this is exactly layer behavior.

This combining operation is controlled by -compose — the recipe for how the top and bottom pixels mix. Its default value is over, which stacks; other values like screen, multiply, and difference produce different color-mixing effects:

compose-screen.sh
magick bg.jpg -gravity center -compose screen logo.png -composite glow.png

-compose screen lightens: dark pixels have almost no effect, bright pixels glow. This is the basic recipe for light and glow effects — two images composited with screen feel like projectors illuminating each other.

Many Images: -layers

When the number of images exceeds two, chained -composite commands become impractical. That's where -layers comes in. ImageMagick treats the image list as a stack of layers, and the -layers option chooses how to stack them:

  • -layers flatten — stacks all frames on one canvas, trimmed to the combined bounding box. Transparent frames are left as holes that reveal the frame below.
  • -layers merge — stacks like flatten, but the canvas isn't trimmed — all areas of all frames are kept.
  • -layers mosaic — like merge, but the canvas is expanded so all frames are visible without being cut off, and the transparent background is filled.
layers-flatten.sh
magick layer1.png layer2.png layer3.png -layers flatten result.png
layers-mosaic.sh
magick a.png b.png c.png -layers mosaic collage.png

The difference between flatten and mosaic is most felt in coordinates. flatten centers the result; mosaic keeps every frame's absolute position on the canvas — useful when you're composing a layout with explicit coordinates. An easy analogy: flatten is tidying a table by stacking everything to the center, mosaic is mounting a wall board according to each piece's plan.

layers-merge.sh
magick -page +100+50 a.png -page +300+120 b.png -layers merge result.png

-page +X+Y before each frame sets its canvas position; -layers merge then unites them all with those positions preserved. This is the basic pattern for assembling a layout from separate components — cards, badges, and text over one background.

Creating Shadows and Glows

Shadows don't need a graphics editor — ImageMagick can create them from any shape. The trick: duplicate the image, blur the copy, darken it, then place it behind.

shadow.sh
magick input.png -background none \
  \( +clone -background black -shadow 60x8+5+5 \) \
  +swap -gravity south -geometry +0+5 -composite result.png

Read from inside the parentheses: +clone duplicates the image, -shadow 60x8+5+5 creates a shadow with 60% opacity, an 8-pixel blur, and a 5-pixel offset to the right-down. +swap swaps the order (the shadow becomes the bottom layer), and -composite unites them. The canvas size is adjusted with -background none so the shadow isn't cut off.

Glow is a shadow with its direction reversed: instead of dark and below, it's bright and spreads in all directions:

glow.sh
magick input.png -background none \
  \( +clone -blur 0x12 -level 50%,100% \) \
  +swap -composite glow.png

-blur 0x12 blurs the copy, -level 50%,100% turns it into a dense bright area, then +swap -composite places it behind the original. The result: the object looks like it's emitting light — the same technique used for badges, buttons, and icons.

Note

The \( ... \) parentheses in an ImageMagick command open a sub-process: the commands inside work on a copy of the image without disturbing the outer sequence. Understanding \( ... \) is the key to building layered compositions without corrupting state.

Masking: Per-Region Editing

The layers above handle image order. To control which part of one image is affected, you need a mask. A mask is a grayscale image that acts as a selective filter: white areas mean "full", black means "none at all", gray means "partial".

The most common use is a transparency gradient:

fade-mask.sh
magick -size 400x300 gradient:white-black mask.png
magick photo.jpg mask.png -alpha off -compose CopyOpacity -composite fade.png

The second command uses -compose CopyOpacity: the image's alpha is taken from the mask's grayscale values. White mask areas become opaque, black areas transparent, and the gradient creates a smooth fade. A photo that was square now fades into transparency — without manually selecting a single pixel.

-mask: A Mask for the Next Operation

Besides CopyOpacity, ImageMagick has the -mask option that binds a mask to the image for the following operation — whatever operation follows only works on the area the mask allows:

blur-masked.sh
magick photo.jpg -mask mask.png -blur 0x5 +mask out.jpg

Here -mask mask.png limits -blur 0x5 to only the white areas of the mask, and +mask releases the mask afterward. The result: only specific parts of the photo are blurred — the rest stays sharp. This is the standard technique for masking faces, blurring license plates, or highlighting objects.

Layered Mask Composition

Masks become far more powerful when combined with layers. Imagine this pipeline: you want to place a logo with a glow effect only in a specific area of the background:

layered-mask.sh
magick background.jpg \
  \( logo.png -resize 150x \( +clone -blur 0x10 \) -swap \
     -composite glow-logo.png \) \
  \( glow-logo.png -mask region.png -composite \) \
  -layers flatten final.jpg

Too complex to read sequentially — and that's exactly the point: professional compositions are built as a chain of sub-processes, masks, and layering. Each set of parentheses is one stage that can be tested separately. Start with the smallest stage, verify the result, then wrap the next stage.

Important

Keep masks as grayscale PNG files. When working with masks, make sure the original image isn't modified before it's used — the order of \( ... \) and +swap determines everything. Build commands from the inside out, and test each layer by writing temporary results to separate files.

Closing

Episode 17 completed the composition foundation: understanding the alpha channel as the requirement for layering, combining two images with -composite and -compose, assembling many frames with -layers flatten, merge, and mosaic, creating shadows and glows with +clone and blur, and editing per-region with masks and -mask.

The core thing to remember: composition is a matter of order and control — order determines what overwrites what, control determines which part is affected. ImageMagick gives you both through the \( ... \) sub-processes, the -layers option, and masks.

In the next episode 18 we dive into advanced geometric transforms: distort — skewing, curving, and projecting images beyond ordinary resize and rotate. See you then!

Learn ImageMagick - Advanced Compositing & Layers | Learn ImageMagick