First, let me mention that if you display an image with alpha transparency using cv2.imshow
then the transparent areas are going to be black.
Since your input image already contains an alpha channel, the solution is simple -- just reuse the alpha channel.
There's a slight problem -- even though PNG format allows to have grayscale with alpha channel, AFAIK there is no way to write such an image with OpenCV.
Therefore the solution is straightforward: Take the processed grayscale image, convert it back to BGR, add the original alpha channel, and save the result.
Since we're in Python, and therefore the image is represented as a numpy array, we can use array indexing to extract the channels we need. numpy.dstack
allows us to add the alpha channel easily.
Sample code:
import cv2
import numpy as np
src = cv2.imread('51IgH.png', cv2.IMREAD_UNCHANGED)
bgr = src[:,:,:3] # Channels 0..2
gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
# Some sort of processing...
bgr = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
alpha = src[:,:,3] # Channel 3
result = np.dstack([bgr, alpha]) # Add the alpha channel
cv2.imwrite('51IgH_result.png', result)
Result:
Once more on different background, so you can see it's really transparent:
imshow
, then the transparent areas will be black (since that's the background used -- the window itself isn't transparent). Next, even though PNG supports grayscale + alpha, I'm not aware of any way to save such an image using OpenCV. – Scholastic