i have a problem where i need to go from this input:
First Input src1 = input2.png
:
applying this src2 = input2.png
as a mask:
to get this output:
I have tried to, but i didnt know how specifiy that the outer color is alpha color (completely transparent) i either endup with black or white.
src1 = cv2imread('input1.png')
src2 = cv2.imread('input2.png')
both same size
print(src2.dtype)
# uint8
dst = cv2.bitwise_and(src1, src2)
cv2.imwrite('res.jpg', dst)
CodePudding user response:
Here is one way to do that in Python/OpenCV.
Just read the second image unchanged. Then copy the alpha channel from the second to the first image.
Image 1:
Image 2:
import cv2
import numpy as np
# read images
img1 = cv2.imread('picture1.png')
img2 = cv2.imread('picture2.png', cv2.IMREAD_UNCHANGED)
# copy alpha from second image to first image
result = img1.copy()
result = cv2.cvtColor(result, cv2.COLOR_BGR2BGRA)
result[:,:,3] = img2[:,:,3]
# save results
cv2.imwrite('picture1_circled.png', result)
Result: