您的位置:首页 > 编程语言 > Python开发

Python opencv添加圣诞帽,不需要微信官方

2017-12-25 23:52 525 查看
圣诞帽,命名为hat.png,注意是png文件



person



code:

import cv2
from PIL import Image
personPath = './xyjy.jpg'
hatPath = './hat.png'

personImg = cv2.imread(personPath)
face_haar = cv2.CascadeClassifier("./haarcascades/haarcascade_frontalface_default.xml")
faces = face_haar.detectMultiScale(pe
b7e3
rsonImg, 1.3, 3)

personImg = Image.open(personPath)
personImg = personImg.convert('RGBA')

hatImg = Image.open(hatPath)
hatImg = hatImg.convert('RGBA')

for face_x,face_y,face_w,face_h in faces:
face_x -= face_w/2
face_y += face_h/2
face_w *= 2
face_h *= 2
hatImg = hatImg.resize((face_w, face_h))
bg = (face_x , face_y - face_h + 100, face_x + face_w, face_y + 100 )
personImg.paste(hatImg, bg, mask = hatImg)

personImg.save('addHat.png')


结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: