title: When pasting an image on another image, how to keep the foreground image transparent?
body: <p>I'm trying to put an image on top of another using OpenCV / Pillow, while keeping the foreground image transparent. If you look at <a href="https://i.imgur.com/mZ7pejB.jpg" rel="nofollow noreferrer">this image</a>, you can see that everything is going smoothly, except I'm not sure how to keep the image transparent.</p>
  <p>I've tried implementing <a href="https://stackoverflow.com/questions/14063070/overlay-a-smaller-image-on-a-larger-image-python-opencv">this StackOverflow solution</a> by <code>@fireant</code> into my program, but nothing seems to work out.</p>
  <p><strong>How can I execute <code>index.py</code> as normal, but keep the foreground png transparent?</strong></p>
  <h2>Images</h2>
  <p><a href="https://i.imgur.com/xBVwf3j.png" rel="nofollow noreferrer">dot_transparent.png</a></p>
  <p><a href="https://i.imgur.com/zKvXwIt.jpg" rel="nofollow noreferrer">image.jpg</a></p>
  <h2>index.py</h2>
  <pre><code>import os
  import numpy
  import cv2
  from PIL import Image
  from os.path import join, dirname, realpath
  import json
  
  def upload_files():
      #https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
      face_cascade = cv2.CascadeClassifier('/Users/matt/Python/LazerEyes/haarcascade_eye.xml')
  
      #https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
      eye_cascade = cv2.CascadeClassifier('/Users/matt/Python/LazerEyes/haarcascade_eye.xml')
  
      img = cv2.imread('new.png')
      img_to_place = cv2.imread('dot_transparent.png')
  
      gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
      gray_to_place = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
      
      img_h, img_w = gray.shape
      img_to_place_h, img_to_place_w = gray_to_place.shape
  
      faces = face_cascade.detectMultiScale(gray, 1.3, 5)
      for (x,y,w,h) in faces:
          roi_gray = gray[y:y+h, x:x+w]
          roi_color = img[y:y+h, x:x+w]
          eyes = eye_cascade.detectMultiScale(roi_gray)
          for (ex,ey,ew,eh) in eyes:
              resized_img = cv2.resize(img_to_place, (eh, ew), interpolation = cv2.INTER_AREA)
              resized_img_h, resized_img_w, _ = resized_img.shape
  
              roi_color[ey:ey+resized_img_h, ex:ex+resized_img_w, :] = resized_img
  </code></pre>