-
Notifications
You must be signed in to change notification settings - Fork 0
/
blend_mode.py
58 lines (40 loc) · 1.55 KB
/
blend_mode.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import cv2 as cv
from blend_modes import blend_modes
mask = cv.imread("mask.png")
foreground = cv.imread("foreground.jpg")
background = cv.imread("background.jpeg")
mask = cv.resize(mask, (256,256))
foreground = cv.resize(foreground, (256,256))
background = cv.resize(background, (256,256))
ratio = mask.astype(float) / 255
f_img = cv.multiply( 1 - ratio, foreground.astype(float))
b_img = cv.multiply( ratio, background.astype(float))
no_alpha_img = cv.add(f_img, b_img)
cv.imshow("normal blend", no_alpha_img/255)
b, g, r = cv.split(mask)
a = np.ones(b.shape, dtype=b.dtype) * 255 #creating a dummy alpha channel image.
mask = cv.merge((b,g,r,a))
b, g, r = cv.split(foreground)
a, _, _, _ = cv.split(mask) #creating a dummy alpha channel image.
foreground = cv.merge((b,g,r, 255 - a))
cv.imshow("a",a)
cv.imshow("foreground", foreground)
b, g, r = cv.split(background)
a = np.ones(b.shape, dtype=b.dtype) * 255 #creating a dummy alpha channel image.
background = cv.merge((b,g,r,a))
cv.imshow("background", background)
opacity = 0.9 # The opacity of the foreground that is blended onto the background is 70 %.
blended_img_float = blend_modes.hard_light(background.astype(float), foreground.astype(float), opacity)
cv.imshow("blended_img_float", blended_img_float/255)
cv.waitKey(0)
"""
mask = mask.astype(float)/255
f_img = cv.multiply( mask, foreground)
b_img = cv.multiply( 1 - mask, background)
no_alpha_img = cv.add(f_img, b_img)
img = cv.add(f_img, b_img)
cv.imshow("no_alpga", no_alpha_img/255)
cv.imshow("img", img/255)
cv.waitKey(0)
"""