title: Python OpenCV - overlay an image with transparency
body: <p>What I'm trying to achieve is to place an image with transparency on top of another one. Something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/njwGd.png" rel="noreferrer"><img src="https://i.stack.imgur.com/njwGd.png" alt="enter image description here"></a></p>
  
  <p>I haven't been able to find any solution, so I decided to go pixel by pixel and calculate the resulting color. That one worked for me, but its very slow.
  I'm new to OpenCV and also to Python. </p>
  
  <p>This is my code, I came up with:</p>
  
  <pre><code>import numpy as np
  import cv2
  
  img1 = cv2.imread("img1.png", -1)
  img2 = cv2.imread("img2.png", -1) # this one has transparency
  h, w, depth = img2.shape
  
  result = np.zeros((h, w, 3), np.uint8)
  
  for i in range(h):
      for j in range(w):
          color1 = img1[i, j]
          color2 = img2[i, j]
          alpha = color2[3] / 255.0
          new_color = [ (1 - alpha) * color1[0] + alpha * color2[0],
                        (1 - alpha) * color1[1] + alpha * color2[1],
                        (1 - alpha) * color1[2] + alpha * color2[2] ]
          result[i, j] = new_color
  
  cv2.imshow("result", result)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>Is there another way of doing this? Some faster way, much faster?
  Thanks.</p>