Home / Blogs

How to Remove the Background of Image Using Rembg in Python?

Python
·

December 20, 2023

how-to-remove-the-background-of-image-using-rembg-in-python

In the world of image editing, removing backgrounds is a common task that can be both time-consuming and challenging. However, with the advent of powerful tools like Rembg, the process has become remarkably simpler.

In this blog, we’ll explore how to use Rembg to effortlessly remove image backgrounds.

Whether you’re a graphic designer, a developer, or just someone looking to enhance your photos, Rembg is a handy tool to add to your arsenal.

What is Rembg?

Rembg is an open-source Python library designed specifically for removing image backgrounds. It utilizes a deep neural network to accurately identify and remove the background from images, saving you valuable time and effort. With just a few lines of code, you can achieve professional-looking results without the need for complex manual editing.

Installation:

Before we dive into the code, let’s start by installing Rembg. Open your terminal or command prompt and run the following command:

pip install rembg

Once the installation is complete, you’re ready to start using Rembg to remove backgrounds from your images.

Basic Usage:

Now, let’s look at a simple example of how to use Rembg in a Python script. Suppose you have an image named “input_image.png” with a white background that you want to remove. Here’s how you can do it:

import rembg
from PIL import Image

def remove_background(input_path, output_path):
    with open(input_path, "rb") as input_file, open(output_path, "wb") as output_file:
        input_data = input_file.read()
        output_data = rembg.remove(input_data)
        output_file.write(output_data)

# Specify input and output paths
input_image_path = "input_image.png"
output_image_path = "output_image.png"

# Remove background
remove_background(input_image_path, output_image_path)

# Display the results
original_image = Image.open(input_image_path)
removed_background_image = Image.open(output_image_path)

original_image.show(title="Original Image")
removed_background_image.show(title="Image with Removed Background")

In this example, the remove_background function takes the path of the input image and the desired output path for the image with the removed background. The Rembg library does the heavy lifting, and the result is saved to the specified output path.

Advanced Options:

Rembg provides additional options for more control over the background removal process. For example, you can adjust the alpha composition or specify a different background color. Here’s an example:

import rembg
from PIL import Image

def remove_background_advanced(input_path, output_path, alpha_matte=False, background_color=(255, 255, 255)):
    with open(input_path, "rb") as input_file, open(output_path, "wb") as output_file:
        input_data = input_file.read()
        
        # Use advanced options
        output_data = rembg.remove(input_data, alpha_matte=alpha_matte, background_color=background_color)
        
        output_file.write(output_data)

# Specify input and output paths
input_image_path = "input_image.png"
output_image_path_advanced = "output_image_advanced.png"

# Remove background with advanced options
remove_background_advanced(input_image_path, output_image_path_advanced, alpha_matte=True, background_color=(0, 0, 0))

# Display the results
removed_background_image_advanced = Image.open(output_image_path_advanced)
removed_background_image_advanced.show(title="Image with Removed Background (Advanced)")

In this example, the remove_background_advanced function allows you to control whether an alpha matte is included and set a custom background color.

To read more about building a custom user model in Django, refer to our blog How to Build a Custom User Model in Django

Conclusion:

Rembg simplifies the process of removing image backgrounds, making it accessible to both beginners and experienced users. With just a few lines of Python code, you can achieve impressive results and enhance your images for various purposes. Experiment with different options and integrate Rembg into your projects to streamline your workflow and take your image editing skills to the next level.

Horilla Editorial Team Author

Horilla Editorial Team is a group of experienced writers and editors who are passionate about HR software. We have a deep understanding of the HR landscape and are committed to providing our readers with the most up-to-date and informative content. We have written extensively on a variety of HR software topics, including applicant tracking systems, performance management software, and payroll software etc. We are always looking for new ways to share our knowledge with the HR community. If you have a question about HR software, please don't hesitate to contact us.