It could be just displaying issue, but it hard to tell from your code sample.
cv2.imshow
doesn't support transparency.
Store the result to PNG image file, and view the image using a viewer that shows transparency. (using GIMP
or paint.net
, it's easy to view the transparent pixels).
The code sample below does the following:
- Loads RGB image and draws black text on the image (load image as BGR).
- Converts image to BGRA.
- Sets value of alpha channel of all back pixels to zero (zero is transparent and 255 is opaque).
The code uses logical indexing instead of using a for loop.
- Stores the BGRA image to PNG image file.
Here is the code:
import cv2
import numpy as np
# Read image in BGR format
img = cv2.imread('chelsea.png')
# Write cat in in black color
cv2.putText(img, 'cat', (img.shape[1]//2-85*len('cat'), img.shape[0]//2+100), cv2.FONT_HERSHEY_DUPLEX, 10, (0, 0, 0), 20)
# Convert from BGR to BGRA
bgra = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
# Slice of alpha channel
alpha = bgra[:, :, 3]
# Use logical indexing to set alpha channel to 0 where BGR=0
alpha[np.all(bgra[:, :, 0:3] == (0, 0, 0), 2)] = 0
# Save bgra to PNG file
cv2.imwrite('bgra.png', bgra)
# Show image for testing
cv2.imshow('img', img)
cv2.imshow('bgra', bgra) # The letters are black, because cv2.imshow does not support transparency.
cv2.imshow('bgra[:,:,3]', bgra[:,:,3])
cv2.waitKey(0)
cv2.destroyAllWindows()
Result:
bgra.png
file:

The site background is white, so the transparent pixels are white...