Python 3 rembg Bulk Processing Script to Remove Background of All Images in a Directory in Terminal
27 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-rembg-bulk-processing-script-to-remove-background-of-all-images-in-a-directory-in-terminal/
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you a
0:05
Python script which will actually remove
0:07
a background from multiple images all
0:10
the images which is present in a folder
0:13
at one time so it's a batch processing
0:15
script it will process each image and
0:17
remove the background and it will create
0:19
the output image so we will take this
0:21
folder as an example so you can see we
0:24
have multiple images so I will run this
0:28
Python script and one by one it will
0:30
remove the background of each image so
0:33
let me just execute the Python script
0:36
here so you will now see it will create
0:40
this folder and process each image at
0:43
one time and it will give you
0:45
notification once the image is completed
0:48
you will see the background will be
0:50
removed
0:57
so for each image you will see it will
1:00
process it patch processing is going on
1:03
and with this way using this script you
1:06
can remove thousands of images
1:08
background at a single time just run
1:10
this script and this script will happen
1:12
in the background and all your images
1:15
background will be removed so for this
1:18
the whole script is given in the
1:20
description of this video so we are
1:22
using a package here so it's a free
1:25
package and the nice thing about this
1:28
this doesn't require any API key you can
1:31
use it unlimited number of times so this
1:33
is an open-source Python package remove
1:36
background so this is actually the
1:38
package here the command is very simple
1:41
you execute this command to install this
1:44
i have already installed it so now let
1:46
me show you from scratch how to actually
1:49
use this and make this
1:54
script so first of all you just need to
1:57
import this
2:01
module
2:04
so we will import the operating system
2:07
module and uh also this remove
2:10
background from this we need to import
2:12
the remove method which is used to
2:15
remove the background of the images and
2:17
one other library we need this pillow
2:20
library which is a image processing
2:22
library also install this and
2:25
then input output so this pillow library
2:30
if you don't know this you can install
2:32
this by executing this command pip
2:34
install pillow so just install this as
2:38
well
2:47
so after that we just need
2:50
to call a function that we will define
2:53
which is
2:56
batch remove backgrounds so this will be
2:59
a function so it actually takes a folder
3:02
which contains all your images and then
3:05
it
3:06
will output underscore images so it
3:09
takes two arguments first of all the
3:10
folder name in which all your images are
3:12
present and the second argument will be
3:14
the actual output folder which it will
3:16
create so let me delete this so we are
3:20
simply providing this images folder so I
3:23
will simply say
3:24
images so now we just need to define
3:27
this
3:30
function and here we'll be creating this
3:33
output
3:35
folder by using the operating system
3:37
module and then we will simply run a for
3:40
loop and in this we will process each
3:43
file uh operating system list
3:46
directory and for each file name first
3:49
of all we will compare right
3:53
here what is the file name extensions so
3:57
right here you'll be having these
3:59
extensions PNG JPG or JPG so if it's a
4:03
file name ending with these extensions
4:05
then we need to process each
4:11
file
4:13
so we get the input path then we
4:17
say the output
4:19
path like this
4:32
so all your output files will be in PNG
4:34
so we will convert all these images so
4:37
you can see the extension will be PNG
4:41
the output file and then we will
4:44
simply open
4:46
each image in write read binary mode and
4:51
then we will process each image remove
4:53
the background so we will read the input
4:58
bytes like this after reading
5:06
this we will basically process
5:11
it and for removing the background we
5:14
will call this remove function which is
5:16
present in this directory we will remove
5:19
the background
5:22
after removing the background we will
5:24
create this
5:25
image creating the image we will use the
5:28
image
5:30
uh constructor image method of the
5:32
pillow library we'll open this
5:36
image and create this output image and
5:38
now we need to just save this output
5:41
image
5:45
save so at the output path location so
5:48
after saving it we will simply write a
5:50
print line that your file has been
5:52
processed so this completes the script
5:55
that's all that we need to do and if I
5:57
just execute the script now you will see
6:00
one by one it will process each image
6:03
remove the background and proceed to the
6:06
next image so this will save you a lot
6:09
of time because many a times you will
6:11
have millions of images you want to
6:13
remove background you can just run this
6:15
script and in the background it will do
6:17
the job for
6:19
you and you can see it is processing
6:22
each image is it is removing the
6:25
background it is making the image
6:27
transparent
6:28
and the speed also is quite good here
6:31
you can see the process is now complete
6:34
it has processed all these images and
6:36
created this folder so this is a very
6:38
handy library it doesn't require any API
6:41
key you can totally use it for free the
6:44
whole script is given in the description
6:47
and also check out my website which is
6:49
freemediatools.com
6:51
uh which contains thousands of tools and
6:54
I will be seeing you in the next one
#Scripting Languages