title: How to create a transparent mask in opencv-python
body: <p>I have sign (signs with arbitrary shape) images with white background and I want to get an image of the sign with transparent background. </p>
  
  <p>I have managed to create a mask and apply it to the image and thought making the mask transparent would be doable. I searched a lot here and elsewhere, but nothing really helped me.</p>
  
  <pre><code>import cv2
  import numpy as np
  
  file_name = "/path/to/input/img/Unbenannt.jpg" # can be also .png
  
  img = cv2.imread(file_name)
  
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  th, threshed = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
  
  kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
  morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel)
  
  _, roi, _ = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  
  mask = np.zeros(img.shape, img.dtype)
  
  cv2.fillPoly(mask, roi, (255,)*img.shape[2], )
  
  masked_image = cv2.bitwise_and(img, mask)
  
  cv2.imwrite("/path/to/output/mask_test.png", masked_image)
  </code></pre>
  
  <p>Input:</p>
  
  <p><a href="https://i.stack.imgur.com/jmXpy.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/jmXpy.jpg" alt="enter image description here"></a></p>
  
  <p>Current Output:</p>
  
  <p><a href="https://i.stack.imgur.com/9THC0.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9THC0.png" alt="enter image description here"></a></p>
  
  <p>As already mentioned I want to make the background transparent.</p>
  
  <p>Help is highly appreciated.</p>