Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/AlphaBlending/alphaBlend.py
3118 views
1
2
# Copyright 2017 by Sunita Nayak <[email protected]>
3
4
import cv2
5
6
# Read the foreground image with alpha channel
7
foreGroundImage = cv2.imread("foreGroundAsset.png", -1)
8
9
# Split png foreground image
10
b,g,r,a = cv2.split(foreGroundImage)
11
12
# Save the foregroung RGB content into a single object
13
foreground = cv2.merge((b,g,r))
14
15
# Save the alpha information into a single Mat
16
alpha = cv2.merge((a,a,a))
17
18
# Read background image
19
background = cv2.imread("backGround.jpg")
20
21
# Convert uint8 to float
22
foreground = foreground.astype(float)
23
background = background.astype(float)
24
alpha = alpha.astype(float)/255
25
26
# Perform alpha blending
27
foreground = cv2.multiply(alpha, foreground)
28
background = cv2.multiply(1.0 - alpha, background)
29
outImage = cv2.add(foreground, background)
30
31
cv2.imwrite("outImgPy.png", outImage)
32
33
cv2.imshow("outImg", outImage/255)
34
cv2.waitKey(0)
35
36