add alpha channel to an image in simpleitk

You can use the VectorIndexSelectionCast function to split an RGB image into separate components. Then create an alpha image of matching dimensions and use the Compose function to combine all four channels into an RGBA image.

Here’s an example

rgb = sitk.Image(100,100,sitk.sitkVectorUInt8)

r = sitk.VectorIndexSelectionCast(rgb, 0)
g = sitk.VectorIndexSelectionCast(rgb, 1)
b = sitk.VectorIndexSelectionCast(rgb, 2)

a = sitk.Image(100,100,sitk.sitkUInt8)

rgba = sitk.Compose(r, g, b, a)
2 Likes