Python 3 rembg Library to Remove Background From Images in Terminal
44 views
Jun 3, 2025
Get the full source code of application here:
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
remove a background of the image uh
0:08
inside Python uh using a free
0:10
open-source package which is uh remove
0:13
background and using this package you
0:16
don't require any API key because most
0:19
of the packages require a API key You
0:22
need to sign up for a subscription on
0:24
the website it cost you money but uh
0:26
recently I found out this package it is
0:28
completely free you can use it unlimited
0:31
number of time so let me show you an
0:34
example right here uh we basically
0:36
provide a input image uh so we have a
0:39
bunch of images present in this folder
0:41
so let me take an example so I provide a
0:44
input image so right inside this Python
0:47
script we provide this
0:50
images
0:52
/ 1 jpg G So this is my input image I'm
0:57
providing this input image So now it
0:59
will remove everything It will make this
1:02
image transparent Let me execute this
1:04
Python script So as soon as I execute
1:07
this you will see it will remove the
1:10
background And it will create this
1:12
output file And you can see the image
1:15
background is completely removed And
1:18
also you can see the quality of the
1:20
rumal It's quite good And uh you can do
1:24
this with any image Let's suppose I take
1:27
a different
1:31
image So once again you
1:39
execute So you will see the output once
1:42
again for this image You will see
1:44
everything in the background Now this
1:47
image contains three persons but you
1:49
will see all the three persons
1:50
background is completely removed So uh
1:54
the package name if you go to this
1:56
website you just need to install this
1:59
first of all So just search
2:02
hereg remove background So this is
2:05
actually the command here Simply execute
2:07
this command to install this module Uh I
2:10
have I have already installed it So now
2:13
let me show you how to it's very simple
2:17
example So first of all we need to
2:19
import this package like this We have
2:22
this remove function and then we also
2:25
need the pillow library which is a image
2:28
processing library and then we import
2:31
the input
2:32
output and then we simply define a
2:35
custom function which will be remove
2:37
background It takes two arguments First
2:39
of all your profile image your input
2:44
image So we take this image and then
2:47
your output image
2:52
So then we define this
2:56
function So so here first of all we read
2:59
this input image We open this input
3:02
image by using the open function So we
3:05
simply provide input path read
3:10
binary and then we read the input bytes
3:15
So we simply say f dot
3:18
read So after that we
3:22
simply remove the background by using
3:26
the remove function which is there in
3:28
this module So we simply call this
3:29
function simply provide the input
3:33
bytes So this will remove the background
3:35
Now we just need to save this image file
3:38
For saving this image we simply use the
3:42
open function of below image
3:46
open And lastly we save this image
3:49
output image dots
3:54
save So that's all So this is a very
3:57
simple script We first of all import the
4:00
package right here This package contains
4:02
this remove function which actually does
4:04
the heavy lifting It removes the
4:06
background from the image Very simple
4:09
module Then we also need the pillow
4:11
library So pip install pillow just
4:14
install this And then we define this
4:17
function We pass two arguments This is
4:19
your input image This is your output
4:20
image So write in this function we open
4:23
the image by using the open function
4:26
Then we read each bite store it in this
4:29
variable Then we call the remove
4:31
function to remove the background And
4:34
then lastly we save this image by first
4:36
of all opening it putting these output
4:39
by it and calling the save function to
4:42
save it So just if I execute now you
4:47
will see it will create this
4:49
result.png file
4:52
So removing your background So the image
4:55
is created It removed the
4:58
background So it's a very handy library
5:00
You can make any sort of web application
5:03
using this in the browser Just allow
5:07
user to upload a image file remove the
5:09
background and download this So in the
5:11
future I will make some videos about
5:14
this how to make web application as well
5:17
using this package But I found this
5:20
package to be very handy So no need to
5:23
subscribe to any model or API key You
5:26
can use this uh module completely free
5:30
And thank you very much for watching
5:31
this video And also check out my website
5:35
freemediatools.com
5:37
uh which contains thousands of tools
#Open Source
#Programming
#Scripting Languages