Blending and masks
blend puts one image over another. Three things decide what you see: how the colors mix (the blend mode), how much of the overlay shows (its alpha, the opacity and a mask), and how the alpha is shared with what is below (the Porter-Duff operator).
import os
import libstb as stb
os.makedirs("out", exist_ok=True)
photo = stb.Image.open("chelsea.png")
badge = stb.Image.open("badge.png")
# A blend mode says how the colors of the overlay mix with the colors below it.
modes = ["normal", "multiply", "screen", "overlay", "difference", "luminosity"]
tiles = [photo.blend(badge, 140, 70, mode=mode).thumbnail(225, 150) for mode in modes]
top = stb.Image.hstack(tiles[:3], gap=6, background="white")
bottom = stb.Image.hstack(tiles[3:], gap=6, background="white")
stb.Image.vstack([top, bottom], gap=6, background="white").write("out/modes.png")
# Opacity weakens the whole overlay.
photo.blend(badge, 140, 70, opacity=0.5).write("out/half.png")
# A mask is a gray image: 255 shows the overlay, 0 hides it. This one fades from left to right.
w, h = badge.width, badge.height
mask = stb.Image(memoryview(bytearray((x * 255) // (w - 1) for y in range(h) for x in range(w))).cast("B", (h, w)))
print("mask:", mask.width, "x", mask.height, ",", mask.channels, "channel")
faded = badge.convert(stb.PixelFormat.RGB).with_alpha(mask)
photo.blend(faded, 140, 70).write("out/faded.png")
print("alpha() gives the mask back:", "yes" if faded.alpha().tobytes() == mask.tobytes() else "no")
# A Porter-Duff operator says how the alpha is shared. destination_out cuts the overlay's shape out of the picture.
photo.convert(stb.PixelFormat.RGBA).blend(badge, 140, 70, op="destination_out").write("out/cutout.png")
# A canvas takes images too, with their own mode and opacity.
canvas = stb.Canvas(photo)
canvas.add_image(badge, 20, 20).blend("multiply").opacity(0.8)
canvas.add_image(badge, 260, 110).blend("screen").opacity(0.8)
canvas.finish().write("out/canvas_images.png")
mask: 160 x 160 , 1 channel
alpha() gives the mask back: yes





Blend modes
These are the modes of the W3C compositing specification, the ones Photoshop and CSS call by the same names, with underscores instead of hyphens. The first nine work on each color channel by itself:
| Mode | What it does |
|---|---|
normal | the overlay replaces what is below (the usual "over") |
multiply | darkens; white changes nothing |
screen | lightens; black changes nothing |
overlay | multiply where the picture is dark, screen where it is light: contrast |
darken, lighten | the smaller or the larger of the two |
add | the sum, at most the maximum |
difference, exclusion | how far apart the two are; exclusion has less contrast |
color_dodge, color_burn | brighten or darken the picture by the overlay |
hard_light, soft_light | overlay with the roles swapped, and a gentler one |
The last four mix the three channels together: hue, saturation, color and luminosity take that part of the overlay and keep the rest of the picture.
Masks
A mask is a gray image of the same size as the overlay: 255 shows the overlay and 0 hides it. with_alpha(mask) gives a picture that mask as its alpha channel, and alpha() gives the alpha channel back as a gray image. The example builds a gradient with plain loops and the badge fades out from left to right.
Operators
The operator says how the overlay and the pixels below share the alpha. source_over is the default and the usual one. destination_out keeps what is below only where the overlay is not, which cuts the overlay's shape out of the picture, as in the third picture. The others are clear, source, destination, destination_over, source_in, destination_in, source_out, source_atop, destination_atop, exclusive_or and plus_lighter. They work inside the rectangle the overlay lands on, and the pixels outside it are not touched.
API at a glance
| Blend | base.blend(over, x, y, mode=, opacity=, op=), a new image |
|---|---|
| A mode | "multiply" |
| An operator | op="destination_out" |
| Give an alpha channel | img.with_alpha(mask) |
| Take the alpha channel | img.alpha() |
| On a canvas | canvas.add_image(img, x, y).blend(mode).opacity(0.8) |