26

I have an image array in RGB space and want to add the alpha channel to be all zeros. Specifically, I have a numpy array with shape (205, 54, 3) and I want to change the shape to (205, 54, 4) with the additional spot in the third dimension being all 0.0's. Which numpy operation would achieve this?

3
  • Do you mean you want an array with rgb values and make the rgb values look like (205, 54, 4, 0)? Please post an example of how the output should be. Commented Sep 22, 2016 at 15:14
  • @Linus OP means the input is a 3D array of dimensions 205 x 54 x 3. Commented Sep 22, 2016 at 15:14
  • @kennytm Correct - the input has shape (205, 54, 3) and I want it to have a shape of (205, 54, 4). As an example, x.shape = (205,54,3) and x[0][0] = [255, 255, 255] and I want x[0][0] = [255, 255, 255, 0]. Commented Sep 22, 2016 at 15:18

4 Answers 4

27

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)) ) )
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I tried a few of them, but couldn't seem to find the right one. np.dstack makes sense.
@kennytm for some reasons I cant understand in my case : data = np.dstack(( data, np.zeros((data[:,:,0].shape)))) return an array of type = float64 instead of uint8. Any clue ?
@pippo1980 specify the dtype in np.zeros. its default value is float64. numpy.org/doc/stable/reference/generated/numpy.zeros.html
Yep thanks I figured out. Didn’t want to edit your answer. Even if I am stuck in the no more question ban !! And don’t know how to recover
10

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.

Comments

10

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)

Comments

5

not sure if your still looking for an answer.

recently i am looking to achieve the exact same thing you are looking to do with numpy because i needed to force a 24 bit depth png into a 32. I agree that it makes sense to use dstack, but i couldnt get it to work. i used insert instead and it seems to achieved my goal.

# for your code it would look like the following:
rgba = numpy.insert(
    rgb,
    3, #position in the pixel value [ r, g, b, a <-index [3]  ]
    255, # or 1 if you're going for a float data type as you want the alpha to be fully white otherwise the entire image will be transparent.
    axis=2, #this is the depth where you are inserting this alpha channel into
)

hope this helps, good luck.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.