Adding alpha channel to RGB array using numpy

You could use one of the stack functions (stack/hstack/vstack/dstack/concatenate) to join multiple arrays together.

numpy.dstack( ( your_input_array, numpy.zeros((205, 54)) ) )

np array style, stack on depth dimension(channel dimension, 3rd dimension):

rgba = np.dstack((rgb, np.zeros(rgb.shape[:-1])))

but you should use OpenCV function:

rgba = cv2.cvtColor(rgb, cv2.COLOR_RGB2RGBA)

If you have your current image as rgb variable then just use:

rgba = numpy.concatenate((rgb, numpy.zeros((205, 54, 1))), axis=2)

Concatenate function merge rgb and zeros array together. Zeros function creates array of zeros. We set axis to 2 what means we merge in the thirde dimensions. Note: axis are counted from 0.

Tags:

Python

Numpy