Remove Image Backgrounds with Python and rembg

A short Python script that shows how to remove the background from an image in Python using the AI-based rembg library, then places the subject on a solid black background with Pillow.

Python projects · Images and AI

Start here

Read the requirements and any configuration below, then follow the usage example. Source paths below are relative to the repository root; run commands from the working directory stated in Usage. This example’s source is in Remove Background/Console_Code/. Install any third-party packages in an isolated Python environment.

Overview

This script takes an input image, removes its background using the AI-powered rembg library, and composites the foreground onto a solid black canvas. The result is saved as a new image file.

Features

  • Automatic background removal through rembg.remove(); the installed dependency chooses its default model
  • Composites the transparent result onto a solid black background
  • Loads the configured image with Pillow and re-encodes it as PNG before background removal; unsupported PNG modes need conversion first
  • The default input and output names differ; keep them distinct to preserve the source image

Prerequisites and installation

rembg[cpu]
Pillow

Install dependencies:

python -m pip install "rembg[cpu]" Pillow

The upstream rembg installation guide documents backend extras and current Python requirements. This repository does not pin a rembg version or explicitly select a model. Its default model, download size, cache location, and model-weight license can therefore change with the installed version. A first run may download weights; later runs can reuse cached weights. Check upstream model terms before using the output commercially.

Usage

By default the script processes the included sample image Elephant.jpg and writes Elephant_Modified.jpg, so you can try it straight away from the Console_Code folder:

python Remove.py

To use your own image, set the input and output paths in Remove.py:

orginal_image = 'path/to/input.jpg'
output_image  = 'path/to/output.jpg'

The processed image will be saved at the output path.

How It Works

  1. The input image is opened and converted to PNG bytes (required by rembg).
  2. rembg.remove() applies the background removal model.
  3. The resulting RGBA image (with transparent background) is pasted onto a new black RGB canvas.
  4. The final image is saved to the output path.

Architecture

Remove Background/
└── Console_Code/
    ├── Remove.py      # Main script
    └── Elephant.jpg   # Sample input image

Troubleshooting and limitations

  • The output background color is currently set to black. Change 'black' to 'white' or any other color in Image.new('RGB', result_img.size, 'black') to use a different background.
  • rembg works best on images with clear subject-background contrast.
  • To keep a transparent background instead, save result_img (the RGBA image) as a .png file rather than compositing it onto a colour.
  • The function’s docstring and variable name mention a white background, but the code uses 'black'.


Read all Python Projects and Code Examples guides online · Project overview · All developer projects