【发布时间】:2021-05-26 03:00:51
【问题描述】:
我正在尝试使用 OpenCV / Pillow 将图像放在另一个图像之上,同时保持前景图像透明。如果你看this image,你可以看到一切都很顺利,除了我不知道如何保持图像透明。
我尝试在我的程序中实现this StackOverflow solution by @fireant,但似乎没有任何效果。
如何正常执行index.py,但保持前景png透明?
图片
index.py
>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
【问题讨论】:
标签: python arrays numpy opencv python-imaging-library