Adding an Alpha Channel to a Monochrome Image Using Python Open CV

I am working on color images (RGB) and color images with alpha channel (RGBA). Reading the alpha channel from an RGBA image is pretty simple, and I can even split 4 channels of the image. Is there any way I can add an alpha channel to a monochrome or grayscale image? In addition, can a separate channel be separately added to channels R, G, B separately?

The code that I use to read a transparent image and split channels looks like this:

import cv2
img = cv2.imread(image1_path,-1)

b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
a = img[:,:,3]

img_merge = cv2.merge((b,g,r,a))
cv2.imshow("img_merge",img_merge)

cv2.imshow("r channel",r)
cv2.imshow("g channel",g)
cv2.imshow("b channel",b)
cv2.imshow("a channel",a)
cv2.waitKey(0)
cv2.destroyAllWindows()

The image I'm using is

RGBA Image

+4
source share
1 answer

2- "luminance-alpha", BGRA, , -. l - :

img_3gray = cv2.merge((l,l,l,a))

- , (, ) , :

img_3blue = cv2.merge((b,b,b,a))

:

img_bzz = cv2.merge((b,z,z,a))

z - .

+4

All Articles