Skip to content

Channels, alpha, and pixel composition

Copy page

Channels are not merely an abstract “colour array”. Each item shows its generated output first, immediately followed by the binding configuration that made it.

The output is a one-band PNG that represents the source image’s red-channel intensity in grayscale.

Channel test graphic showing red-channel intensity in grayscale
Red channel · PNG640 × 360, 4,693 bytes, 1 bands

Generated with govips v2.19.0 / libvips 8.17.3.

if err := image.ExtractBand(0, 1); err != nil { return err }
return exportPNG(image, "red-channel.png")

Alpha is exported as a one-band PNG so that the mask itself can be inspected instead of treating it as RGB.

Channel test graphic showing transparency as a grayscale alpha channel
Alpha channel · PNG640 × 360, 5,301 bytes, 1 bands

Generated with govips v2.19.0 / libvips 8.17.3.

if err := image.ExtractBand(3, 1); err != nil { return err }
return exportPNG(image, "alpha-channel.png")

The govips code explicitly splits bands and calls BandJoin. sharp’s ensureAlpha() guarantees an alpha-bearing output, but it is not a same-named per-band composition API. The semantics should not be treated as fully equivalent.

Transparent test graphic rebuilt from its colour and alpha channels
Rebuilt RGBA · PNG640 × 360, 10,175 bytes, alpha, 4 bands

Generated with govips v2.19.0 / libvips 8.17.3.

bands, err := image.BandSplit()
if err != nil { return err }
defer closeAll(bands)
if err := bands[0].BandJoin(bands[1:]...); err != nil { return err }
return exportPNG(bands[0], "rebuilt-rgba.png")

The transparent image is flattened against #f8fafc before WebP export. Choose a background explicitly before exporting to a format without alpha.

Channel test graphic with transparency flattened over a light background
Flattened alpha · WEBP640 × 360, 3,672 bytes, 3 bands

Generated with govips v2.19.0 / libvips 8.17.3.

if err := image.Flatten(&vips.Color{R: 248, G: 250, B: 252}); err != nil { return err }
return exportWebp(image, "flattened.webp")

sharp also exposes high-level threshold(), boolean(), dilate(), and erode() methods. govips currently has no same-named public wrappers, so this page does not present them as cross-binding equivalents.