Home > OS >  how to cut picture with circle mask that has alpha color?
how to cut picture with circle mask that has alpha color?

Time:03-24

i have a problem where i need to go from this input:

First Input src1 = input2.png :

enter image description here

applying this src2 = input2.png as a mask:

enter image description here

to get this output:

enter image description here

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:

enter image description here

Image 2:

enter image description here

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:

enter image description here

  • Related