Skip to main content

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")
Output
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 to gray
convert to gray
red and blue swapped
red and blue swapped
blend, normal
blend, normal
blend, multiply at 0.8
blend, multiply at 0.8
flatten onto white
flatten onto white
flatten onto blue
flatten onto blue

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 countimg.convert(stb.PixelFormat.GRAY)
Splitimg.split(), a tuple of images
Mergestb.Image.merge([b, g, r])
Blendbase.blend(over, x, y, mode=, opacity=), returns a new image
Flattenimg.flatten((r, g, b))
The alpha as an imageimg.alpha()