Removing black background and make transparent from grabcut output in python open cv : <p>I have been trying to remove the black background from the grabcut output using python opencv. </p>

<pre><code>import numpy as np
import cv2

img = cv2.imread(r'myfile_1.png')
mask = np.zeros(img.shape[:2],np.uint8)

bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)

rect = (1,1,665,344)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)

mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]

cv2.imshow('img',img)
cv2.imwrite('img.png',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code></pre>

<p>Above code I had written to save the grabcut output. Please suggest, How I can remove the black background and make it transparent?</p>

<p><a href="https://i.stack.imgur.com/M4rgG.png" rel="noreferrer"><img src="https://i.stack.imgur.com/M4rgG.png" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/2lAK2.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2lAK2.png" alt="enter image description here"></a></p>
