I am trying to overlay a transparent PNG over a JPG image, I somehow cannot get it to work, what I tried:
import cv2
import numpy as np
a = cv2.imread("a.jpeg")
b = cv2.imread("b.png", cv2.IMREAD_UNCHANGED)
# add alpha channel to jpeg
(h, w) = a.shape[:2]
a = np.dstack([a, np.ones((h, w), dtype="uint8") * 0])
c = cv2.add(b,a)
cv2.imwrite("out.jpeg", c)
The following code will combine the two images, but the PNG transparency is not correct, it's somehow more opaque than it should be. ( I read this could be a issue with opencv ? not reading alpha correctly from png )
What I am trying to do is simply stack two image over another, a background JPG and put over a PNG that has some transparent zones, both images are the same size.
Thanks!
np.ones(...) * 0
– why do you not usenp.zeros
? And maybe this is the cause of the problem, where you mix up an alpha channel containing ones vs. zeros (transparent vs. opaque)? – mkrieger1