Python 3 rembg Script to Replace Background of Image With Custom Image Using OpenCV in Terminal
19 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-rembg-script-to-replace-background-of-image-with-custom-image-using-opencv-in-terminal/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a Python
0:04
script which will actually uh remove the
0:08
background of a image and replace with
0:10
new background so we have a simple
0:14
example right here let me show you so
0:16
there will is a original image right
0:18
here you'll see a person is there with a
0:21
background now we'll be removing this
0:23
background and replacing it with a new
0:25
background which is this one which is
0:27
actually a desert
0:30
JPG so now we run this Python script and
0:35
as soon as a Python script runs you will
0:37
see a new image will be created and with
0:40
that new background will be added to
0:43
that
0:45
image so you will see custom background
0:48
applied and saved as output.png so this
0:51
file image file is created and as you
0:54
can see the background the previous
0:56
background has been removed and it has
0:58
been
0:59
added a new background has been added so
1:02
I will show you how to do this for
1:04
completely free inside
1:06
Python so we will be using uh quite a
1:09
number of packages which will be
1:10
completely free so first of all let me
1:14
show you the packages
1:17
so the first package is remove
1:20
background so this will actually remove
1:22
the background the original background
1:24
from the image so this is actually the
1:26
free open-source package so first of all
1:29
you need to install this the second
1:31
package we need to install is uh open
1:37
cvython which is also a very popular
1:40
package related to image so also we need
1:43
to install this as well
1:47
and I think we also need to install
1:50
pillow package which is again a image
1:52
processing
1:54
package the command is simple you need
1:56
to install this as well and lastly we
1:59
also need to install numpy which is
2:02
a array computing package so need to
2:06
install these four packages and then we
2:08
can start building this so let me just
2:11
delete this image and start just create
2:14
app py file and I have given the script
2:16
in the description of this video so
2:19
first of all we will require all these
2:21
packages from remove background we need
2:24
to
2:24
import the remove method which will
2:27
remove the background of the image and
2:29
then from this pillow package we need to
2:31
import the image
2:35
constructor and then from this numpy
2:37
package we need to
2:40
import SNP and then the open cv package
2:44
so we imported all the four packages
2:46
after that we just need to specify the
2:48
input image path so our input image is
2:52
located in the same
2:54
directory so it's simply provide this
2:56
and then we provide the background image
3:00
path so what will be the background
3:03
image so we just need to provide this as
3:08
well and then we provide the output
3:12
image
3:13
path so this will also be created in the
3:16
same directory now what we need to do we
3:19
need to convert
3:20
this
3:23
foreground into we will use the numpy
3:26
package and use the array method and
3:28
here we
3:29
specify the removed
3:36
uh yeah so first of all we need to
3:38
remove the background of the image for
3:40
removing the background we create this
3:44
new variable remove bg image and we
3:48
simply call this function that we
3:49
imported from this package remove so
3:52
this will simply remove the background
3:54
of the image original background so here
3:56
we just need to provide the input
3:59
image input image path so this will
4:01
remove the background and then we need
4:03
to simply get the foreground from this
4:06
we simply call np array on this remove
4:09
background
4:11
image after that we get the dimensions
4:14
of the foreground such as the height and
4:17
the
4:21
width so we simply say foreground dot
4:25
shape and here we specifically get this
4:29
after this we need to load and resize
4:31
the background for doing this we just
4:34
need to
4:35
say background and open CV will be
4:38
loading this image by image read package
4:41
and here we specify the background image
4:44
path and we will simply check here that
4:47
if background is none then we will
4:51
simply
4:53
raise background image not
4:56
found but if the background is a
4:58
variable then we just need
5:00
to convert this
5:03
into R RGB color so like this after that
5:09
we just need to apply or resize it so we
5:12
call the resize method of open CV and we
5:16
place this background with
5:18
the original image width and the height
5:23
so after this we just need to add the
5:27
alpha channel to background if missing
5:31
so if background
5:34
shape 2 is equal to
5:38
three then we just need to add this
5:41
background np
5:45
dstack so this will be quite little bit
5:48
complicated you just need to copy paste
5:50
all this code that I'm writing so this
5:52
is simply placing that uh new background
5:55
to the original image after that we just
5:58
need to composite or replace the
6:00
transparent areas with the background so
6:02
for this we just say
6:07
foreground so this is again composing
6:10
the original background with the new
6:12
background and then after that we just
6:15
need to save this result for saving the
6:17
image you simply say image from
6:20
array combined and save at that location
6:24
and then see simply print your output
6:28
image has been created so that's all the
6:30
script here if you don't understand
6:32
fully the script you just need to see
6:37
the result here and then the script is
6:39
available in the description of the
6:41
video so don't need to worry about it
6:47
so if I execute this
6:51
now we just need to I forgot to place
6:55
the colon here so
7:00
again so you will see it is saying
7:04
that some problem occurred let me see at
7:07
line number 262
7:12
that line
7:15
uh I think it is creating problem at
7:19
that location is if
7:24
background yeah I think here let me
7:28
replace
7:30
it sorry this is raise exception
7:34
not value error so that was a problem so
7:41
So if you just execute the script here
7:51
uh the overall script is given in the
7:54
description guys i think there is some
7:56
kind of typo mistake which happened let
7:59
me paste the original
8:01
script and execute this
8:09
you will now see the original background
8:11
has been applied this is original image
8:14
this is the new image created and you
8:16
can take this example with any
8:18
background
8:26
so let's suppose I applied this with
8:46
this so you can see that so you can take
8:49
any background and apply this with your
8:51
image with this script and I think the
8:54
result is quite tremendous for this you
8:57
can see we haven't used any API these
9:00
are all four free modules remove
9:03
background pillow numpy and open cv so
9:06
with this script you can create web
9:07
applications as well taking the input
9:09
image and background and you can replace
9:12
your image with a new background with
9:14
this script so the script is given in
9:16
the description and also check out my
9:19
website freemediattools.com
9:22
uh which contains thousands of tools