Channels and alpha
A picture has 1 channel (gray), 2 (gray and alpha), 3 (RGB) or 4 (RGBA). Alpha says how opaque a pixel is: 255 is fully opaque and 0 is invisible. The badge in this example is the one sample picture that has it, a disc with a soft rim.
import os
import libstb as stb
os.makedirs("out", exist_ok=True)
badge = stb.Image.open("badge.png") # RGBA: a disc with a soft, transparent rim
photo = stb.Image.open("chelsea.png") # RGB
print("badge has", badge.channels, "channels, photo has", photo.channels)
# convert() changes the channel count.
gray = photo.convert(stb.PixelFormat.GRAY)
rgba = photo.convert(stb.PixelFormat.RGBA)
print("alpha that convert adds:", rgba.tobytes()[3])
print("channels after dropping alpha:", badge.convert(stb.PixelFormat.RGB).channels)
# split() gives one single-channel image per channel, merge() puts them back in any order.
r, g, b = photo.split()
swapped = stb.Image.merge([b, g, r])
print("split into", len(photo.split()), "images")
# blend() lays one image over another at (x, y): the usual "over". The overlay may hang over the edge.
placed = photo.blend(badge, 20, 30)
shaded = photo.blend(badge, 200, 30, mode="multiply", opacity=0.8)
# flatten() blends the alpha onto a color and drops it.
on_white = badge.flatten()
on_blue = badge.flatten((20, 40, 160))
print("flatten:", badge.channels, "->", on_white.channels, "channels")
gray.write("out/gray.png")
swapped.write("out/swapped.png")
placed.write("out/placed.png")
shaded.write("out/shaded.png")
on_white.write("out/on_white.png")
on_blue.write("out/on_blue.png")
badge has 4 channels, photo has 3
alpha that convert adds: 255
channels after dropping alpha: 3
split into 3 images
flatten: 4 -> 3 channels






Convert
convert gives the picture the channel count you ask for. Going from RGB to RGBA adds an alpha channel of 255, fully opaque. Going from RGBA to RGB simply drops the alpha, which is not what you want when the picture has transparent parts: for those, use flatten. Going to gray takes 0.299 R + 0.587 G + 0.114 B of the stored values, with no gamma step, as Pillow does.
Split and merge
split returns one single-channel image per channel, and merge builds an image from single-channel ones in whatever order you give. Passing blue, green, red swaps the two ends, as in the second picture.
Blend
blend(overlay, x, y) lays the overlay over the image at a position, using the overlay's alpha. The overlay may hang over the edge, and only the part inside lands. A blend mode changes how the colors mix (multiply darkens, screen lightens, and there are more), and an opacity from 0 to 1 weakens the whole overlay. The result keeps the channel count of the base picture.
More on blend modes and masks is on blending and masks.
Flatten
flatten(background) blends the alpha onto a color (white by default) and drops the channel. Use it before writing a transparent picture as a JPEG.
API at a glance
| Change the channel count | img.convert(stb.PixelFormat.GRAY) |
|---|---|
| Split | img.split(), a tuple of images |
| Merge | stb.Image.merge([b, g, r]) |
| Blend | base.blend(over, x, y, mode=, opacity=), returns a new image |
| Flatten | img.flatten((r, g, b)) |
| The alpha as an image | img.alpha() |