How to Remove Image Background in Python Django?

Published On: 03/11/2022 | Category: Django


Hi Dev,

In this tutorial, I will show you how to remove image background in django. if you want to see example of django remove image background example tutorial then you are a right place. it's simple example of how to remove image background in python django. you can understand a concept of how to remove image background using rembg library django. You just need to some step to done django remove image background example.

You may have thought, "I wish I could get rid of that background," and searched for a picture background remover. You discovered that there are internet tools available, but you want the uploading and saving of photos to be automated.

We are grateful for the free, open-source rembg package, which uses only a few lines of Python code to address these issues.

Here i explained simply step by step example of how to remove image background in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 manage.py startapp core

Step 3: Update setting.py

Then update INSTALLED_APPS within our settings.py file to notify Django about the app.

settings.py

....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]

Step 4: Installing the rembg library

In this section first of all we need to install You can install the library using the pip installer as library through below following command:

pip install rembg
pip install rembg[gpu]

Step 5: Creating the Views

In this step, we need to configure views. The remove_bg page will just be a template. function is basically remove background in original image the open the core/views.py file and add:

core/views.py
from django.shortcuts import render
from rembg import remove
from django.http import HttpResponse

# Create View
def remove_background(source_image_path, destination_image_path):
    try:
        with open(source_image_path, 'rb') as i, open(destination_image_path, 'wb') as o:
            # read source image in binary format
            input = i.read()
            # apply remove function to remove background
            output = remove(input)
            # save the new image in destination path
            o.write(output)
        print("Image background removed successfully")
    except Exception as e:
        print(f"There is problem while processed -> {e}")

def remove_bg(request):
    # call function
    remove_background("lion.jpeg", "only-lion.jpeg")
    return HttpResponse("Image background removed successfully!")

Step 6: Creating URLs

In this section, we need a urls.py file within the core app however Django doesn't create one for us with the startapp command. Create core/urls.py with your text editor and paste below code.

core/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('remove-bg', views.remove_bg),
]

Next, we require to add a URL path for our example app which can be done by importing include and setting a path for it.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python manage.py runserver

Next, go to the http://localhost:8000/remove-bg address with a web browser.

Original Image:

Output Image:

I hope it will help you....

Happy Coding!