Python 3 OpenCV Script to Replace Background of Video With Solid Color Green Screen in Terminal
153 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-opencv-script-to-replace-background-of-video-with-solid-color-green-screen-in-terminal/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you how to
0:04
change the background of the video with
0:08
a solid color so you can see this is
0:10
actually a original video with the
0:11
original background here gray background
0:14
so I will actually change this
0:16
background with a solid
0:19
color let's suppose a green green screen
0:22
it can be a blue color so let me show
0:24
you the output the output will will look
0:28
something like this a green screen video
0:30
you can easily create a green screen
0:31
video you can replace the background of
0:34
the video with a solid color it can be
0:36
green it can be red it can be blue so in
0:39
this video I will show you a Python
0:41
script which does this automatically and
0:44
uh for doing this we need some packages
0:47
open-source packages so the very first
0:50
package we need is Open
0:53
CV-Python so the command is simple
0:55
simply execute this command to install
0:58
this package the second package is
1:00
required is media pipe which is also a
1:03
very good package
1:05
for changing the background of the video
1:08
so install this package as well it
1:10
contains various data models and thirdly
1:13
you will also need this package numpy
1:16
which is array computing
1:19
package not this one this
1:23
package so just install these three
1:26
packages after that just create a app py
1:29
file and the full script is given in the
1:31
description of this video so first of
1:34
all I will import all the three packages
1:37
open cv media pipe and numpy
1:43
after importing it we need to initialize
1:45
the media
1:49
pipe
1:54
package like
1:57
this after that we just need to
2:05
say need to call this model selection
2:08
one so in this way we can initialize the
2:10
media pipe library we are passing this
2:13
model which is
2:14
one it contains various models but we
2:17
using the basic model one and then we
2:19
need to initiate
2:22
or
2:24
or initialize the video for this we use
2:27
the video capture method of open CV and
2:30
here we specify whichever video we are
2:33
working so we are loading the video that
2:35
we are input video so this is the input
2:38
video so we are passing this to this
2:39
function after that we initialize the
2:42
width height and the
2:45
FPS using these
2:49
methods after that we initialize another
2:52
this
2:54
one again for this is MP4 video so here
2:57
we are passing MP4 and then we are
3:00
writing this output video which will get
3:02
created output green background
3:05
MP4 and passing all this and here you
3:09
can select the green color it can be any
3:11
color so you can select for green we
3:14
write here 0
3:16
255
3:17
25 so this is green background but here
3:20
you can change to any color let's
3:23
suppose I make it a red color video so
3:26
here you will provide this RGB color
3:29
after that you run this file
3:32
loop we do this for each second each
3:35
frame in the video and
3:39
uh we read each frame
3:47
and
3:49
so we read each
3:52
frame by open CV method
3:59
after that we get the
4:02
result segment
4:04
process and then we mass result dot
4:08
segmentation
4:11
mask and then we use this condition is
4:14
equal to mask greater than
4:17
0.5
4:19
and this we are
4:22
using numpy library and Then we put this
4:26
solid color background in the background
4:29
of the video with these two
4:32
lines like
4:34
this and then we combine the map the
4:37
mask and then write this video with
4:40
these two lines of
4:42
code we are writing this output video
4:46
and then
4:47
we release all the resources and print
4:50
out a statement that your video has been
4:52
generated so that's all if you don't
4:55
understand fully the code it's normal
4:57
it's little bit complicated code but I
5:00
have given the script in the description
5:02
so once you get the code you can better
5:06
understand so if I execute this script
5:10
now you will see it will take some time
5:13
around about uh if you're running it for
5:15
the very first time it will take around
5:17
30 seconds and then it will process your
5:19
video it depends upon how much long is
5:22
the video in my case it's 17 second long
5:25
video so it will not take much time so
5:28
it's 15 second video so if your video is
5:32
large then it will take some large time
5:35
so here we are just providing a red
5:37
color if you
5:39
check so the background should change to
5:42
red color so just wait for the process
5:45
to
5:45
finish and in this way you can put any
5:49
solid background color it can be a green
5:51
screen it can be a red blue color so
5:56
it's very easy to do using
6:02
Python so just wait for the process to
6:05
finish and then we can show you the
6:13
result you can see it is using
6:15
TensorFlow this media pipe library is
6:18
depending upon TensorFlow it is using
6:20
this package as well
6:23
so so media pipe is a
6:26
very complicated but still a very
6:29
powerful library when it comes to
6:30
changing the video
6:33
background uh you can now see it has
6:37
completed and uh if I show you the
6:40
output
6:44
here you can see the output changes to
6:47
uh blue
6:49
color that was the original video this
6:53
was the original video and it changes
6:56
the whole background of the video to a
6:58
solid color which is blue color so in
7:01
this easy way you can change to any
7:04
color you just need to provide the value
7:07
inside the RGB so right
7:11
here this is for blue color and thank
7:15
you very much for watching this video
7:17
and also check out my website
7:18
freemediatools.com
7:20
uh which contains thousands of tools
#Online Media
#Online Video
#Scripting Languages