Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • PDF Invoice Generator
Menu

Python 3 Flask Project to Remove Background of Multiple Images Using Rembg Library in Browser

Posted on May 18, 2023

 

 

Welcome folks today in this blog post we will be using the flask web framework to remove background of multiple images using the rembg library. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the below libraries using the pip command as shown below

 

 

pip install flask

 

 

pip install rembg

 

 

And after that you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import os
import io
import zipfile
from flask import Flask, render_template, request, send_file
from rembg import remove
 
app = Flask(__name__)
 
@app.route('/')
def index():
    return render_template('index.html')
 
if __name__ == '__main__':
    app.run(debug=True)

 

 

As you can see we are loading the index.html template when the user goes to the / route and then we are loading all the libraries at the top. And now we need to create the templates folder and inside it create the index.html file and copy paste the following code

 

 

templates/index.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
    <title>Image Background Removal</title>
</head>
<body>
    <h1>Image Background Removal</h1>
    <form action="/process" method="post" enctype="multipart/form-data">
        <input type="file" name="images" multiple>
        <input type="submit" value="Remove Background">
    </form>
</body>
</html>

 

 

As you can see we have the simple html5 form where user can select the images to remove background and then we are making the post request to the /process endpoint. And now we need to define this post request inside the app.py file as shown below

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@app.route('/process', methods=['POST'])
def process_images():
    # Create a temporary directory to store the processed images
    temp_dir = 'temp_images'
    os.makedirs(temp_dir, exist_ok=True)
 
    # Get the uploaded files from the request
    files = request.files.getlist('images')
 
    # Process each uploaded image
    for file in files:
        # Read the image data
        img_data = file.read()
 
        # Remove the background
        output_data = remove(img_data)
 
        # Save the processed image in the temporary directory
        filename = file.filename
        output_path = os.path.join(temp_dir, filename)
        with open(output_path, 'wb') as f:
            f.write(output_data)
 
    # Generate a zip file containing the processed images
    zip_path = 'processed_images.zip'
    with zipfile.ZipFile(zip_path, 'w') as zip_file:
        for root, _, files in os.walk(temp_dir):
            for file in files:
                file_path = os.path.join(root, file)
                zip_file.write(file_path, file)
 
    # Clean up the temporary directory
    for root, _, files in os.walk(temp_dir):
        for file in files:
            os.remove(os.path.join(root, file))
    os.rmdir(temp_dir)
 
    # Send the zip file as a response for download
    return send_file(zip_path, as_attachment=True)

 

 

As you can see we are first of all removing the background of all the images which are selected and then we are making the zip file and storing the output files and downloading it as an attachment inside the browser.

 

Recent Posts

  • Node.js Express Project to Remove Background of Images Using Rembg & Formidable Library in Browser
  • Node.js Tutorial to Remove Background From Image Using Rembg & Sharp Library in Command Line
  • Python 3 Flask Project to Remove Background of Multiple Images Using Rembg Library in Browser
  • Python 3 Rembg Library Script to Bulk Process Multiple Images and Remove Background in Command Line
  • Python 3 Rembg Library Script to Remove Background From Image in Command Line
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme