File size: 727 Bytes
b921188
 
ddb17d6
ade4c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import cv2
import numpy as np


def overlay_hairstyle(img, hairstyle_img, mask, x_offset=0, y_offset=0, scale=1.0):
# Resize the hairstyle
h, w = img.shape[:2]
hs = cv2.resize(hairstyle_img, (0, 0), fx=scale, fy=scale)
hh, hw = hs.shape[:2]


# Position
center_x, center_y = w // 2 + x_offset, h // 5 + y_offset
top_left_x = max(0, center_x - hw // 2)
top_left_y = max(0, center_y - hh // 2)


roi = img[top_left_y:top_left_y+hh, top_left_x:top_left_x+hw]
if roi.shape[:2] != hs.shape[:2]:
return img


if hs.shape[2] == 4:
alpha = hs[:, :, 3] / 255.0
for c in range(3):
roi[:, :, c] = roi[:, :, c] * (1 - alpha) + hs[:, :, c] * alpha
else:
roi[:] = hs


img[top_left_y:top_left_y+hh, top_left_x:top_left_x+hw] = roi
return img