Last Updated : 23 Jul, 2025
Prerequisite: Addition and Blending of images using OpenCV in Python
In this article, we will be showing you how to effectively blend two videos together. But for that, we will first see what is Alpha Blending.
Alpha BlendingAlpha blending is a curved mix of two tones taking into consideration transparency impacts in computer illustrations. The estimation of alpha in the color code goes from 0.0 to 1.0, where 0.0 addresses a completely transparent tone, and 1.0 addresses a completely dark tone. The estimation of the subsequent color when color Value1 with an alpha value of Alpha is drawn over a foundation of color Value0 is given by:
Value = Value0(1.0 - Alpha) + Value1(Alpha)
The alpha part might be utilized to mix to red, green and blue segments similarly, as in 32-bit RGBA, or, then again, there might be three alpha qualities determined relating to every one of the essential tones for spectral tone shifting.
Now, the second thing we will be using is OpenCV which is a library of programming functions that focuses on real-time computer vision. But, before moving forward we will keep a note of a few things.
Important PointsIn order for our program to work perfectly, you've to make sure:
We will be taking two videos as input, one will be our background video and the second one will be our foreground video. The main work is to remove the solid color background of this foreground video so that we're left with the subject only (of foreground video) with its transparent background. After making this change, the foreground video will then be put on the background video, which will give an illusion of a single video with some effect on it but actually, there will be two videos blended together. This magic is done with the help of OpenCV and Python.
Step-by-step ApproachInput:
Foreground Video
Background Video
Notice that the foreground video contains only the particles with a solid black background, thus for better results, it is preferable to take foreground videos and background videos like these.
Python3
# importing necessary packages
import numpy as np
import cv2
# assigning path of foreground video
path_1 = r"C://Users//Lenovo//Desktop//Python Workshop//z.mp4"
fg = cv2.VideoCapture(path_1)
# assigning path of background video
path_2 = r"C://Users//Lenovo//Desktop//Python Workshop//v.mp4"
bg = cv2.VideoCapture(path_2)
h, w = 1080, 1920
while True:
# Reading the two input videos
# we have taken "ret" here because the duration
# of bg video is greater than fg video,
ret, foreground = fg.read()
# if in your case the situation is opposite
# then take the "ret" for bg video
_, background = bg.read()
# if foreground array is not empty which
# means actual video is still going on
if ret:
# creating the alpha mask
alpha = np.zeros_like(foreground)
gray = cv2.cvtColor(foreground, cv2.COLOR_BGR2GRAY)
alpha[:, :, 0] = gray
alpha[:, :, 1] = gray
alpha[:, :, 2] = gray
# converting uint8 to float type
foreground = foreground.astype(float)
background = background.astype(float)
# normalizing the alpha mask inorder
# to keep intensity between 0 and 1
alpha = alpha.astype(float)/255
# multiplying the foreground
# with alpha matte
foreground = cv2.multiply(alpha,
foreground)
# multiplying the background
# with (1 - alpha)
background = cv2.multiply(1.0 - alpha,
background)
# adding the masked foreground
# and background together
outImage = cv2.add(foreground,
background)
# resizing the masked output
ims = cv2.resize(outImage, (980, 540))
# showing the masked output video
cv2.imshow('Blended', ims/255)
# if the user presses 'q' then the
# program breaks from while loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# if the actual video is over then there's
# nothing in the foreground array thus
# breaking from the while loop
else:
break
print('Video Blending is done perfectly')
Output:
Notice that in this output video we can see that the sand is moving in the air in a hot desert area, which in reality is the blended output of the particle video(used as foreground) and desert video(used as background). Also, you can always press "q" from your keyboard to break the loop and quit the program.
So in this way, you can blend two videos and if done correctly, this method can also provide a professional edit look as well.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4