How to Remove the Background of an Image Using Python ?

Introduction

Recently, I wanted to remove the background color from a photo I planned to use as a profile picture on my website. I didn’t want a distracting background — just a clean, transparent cutout of myself. Instead of relying on online tools, I decided to explore how to do it programmatically using Python.

In this article, I’ll walk through several effective ways to remove the background from an image using Python.

The rembg library is an easy-to-use and efficient solution that leverages deep learning models to remove image backgrounds with high accuracy.

Installation

First, install rembg and its dependencies:

1
2
pip install rembg
pip install pillow numpy

Code Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from rembg import remove
from PIL import Image

# Load input image
input_path = "input_image.jpg"
output_path = "output_image.png"

image = Image.open(input_path)
output = remove(image)

# Save the output image with transparent background
output.save(output_path)
print("Background removed successfully!")

How to Remove the Background of an Image Using Python ? How to Remove the Background of an Image Using Python ?
How to Remove the Background of an Image Using Python ?

This method preserves the subject of the image while making the background transparent.

Advantages:
- Automatically detects background and foreground.
- Handles complex scenes well.
- Outputs an image with a transparent background — perfect for web use.

If you’re not sure what color your background is, or if it’s cluttered, rembg is the easiest and most effective solution.

Replace Transparent Background with Gray (or Any Color)

To replace the transparent background with a gray color (e.g., RGB = (200, 200, 200)):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from rembg import remove
from PIL import Image

# Step 1: Load and remove background
input_image = Image.open("ben_2024.png").convert("RGBA")
output_image = remove(input_image)

# Step 2: Create a gray background
gray_bg = Image.new("RGBA", output_image.size, (200, 200, 200, 255))

# Step 3: Composite the subject over the gray background
final = Image.alpha_composite(gray_bg, output_image)

# Step 4: Save the result as standard RGB (if needed)
final.convert("RGB").save("your_photo_gray_bg.jpg")

You can replace (200, 200, 200, 255) with any RGB color — for example:

  • White: (255, 255, 255, 255)
  • Black: (0, 0, 0, 255)
  • Blue: (0, 120, 255, 255)

How to Remove the Background of an Image Using Python ?
How to Remove the Background of an Image Using Python ?

Method 2: Using OpenCV for Color-Based Removal

To remove the background of an image using OpenCV (cv2), you typically rely on color segmentation — a method that works well when the background is a uniform or easily separable color (like green screen, white walls, blue sky, etc.).

This example assumes your background is light (e.g., white) and your subject is darker or more colorful.

Installation

Install OpenCV and NumPy:

1
pip install opencv-python numpy

Code Implementation

 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
import cv2
import numpy as np

# Load the image
image = cv2.imread("your_photo.jpg")

# Convert to HSV (Hue, Saturation, Value) color space
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# Define background color range (here: white background)
lower = np.array([0, 0, 200])     # Lower HSV bound for white
upper = np.array([180, 25, 255])  # Upper HSV bound for white

# Create a mask for the background
mask = cv2.inRange(hsv, lower, upper)

# Invert mask to keep the subject
mask_inv = cv2.bitwise_not(mask)

# Apply the mask to extract the subject
foreground = cv2.bitwise_and(image, image, mask=mask_inv)

# Optional: Add transparency to background
# Convert to BGRA (add alpha channel)
foreground_bgra = cv2.cvtColor(foreground, cv2.COLOR_BGR2BGRA)

# Set fully transparent where the background was
foreground_bgra[:, :, 3] = mask_inv

# Save result
cv2.imwrite("output_no_bg.png", foreground_bgra)
print("Background removed and saved as output_no_bg.png")

How to Choose the Right HSV Range

  1. Use an image editor (GIMP, Photoshop) or OpenCV GUI tools to inspect HSV values of the background.
  2. Use: cv2.cvtColor(pixel_bgr_value, cv2.COLOR_BGR2HSV) to test color values interactively.

Example: For a green screen, you might use:

1
2
lower = np.array([35, 40, 40])
upper = np.array([85, 255, 255])

Output Format Tips

  • Use .png format to preserve transparency (JPG doesn't support it).
  • If you don’t need transparency, you can fill the removed background with a solid color (like gray or white) using cv2.addWeighted() or NumPy.

Limitations

  • Doesn't work well with complex or textured backgrounds.
  • Best for controlled lighting and background colors (studio photos, product images, etc.).
  • For profile pictures with busy backgrounds, use AI-based tools like rembg.

How to Remove the Background of an Image Using Python ?
How to Remove the Background of an Image Using Python ?

Use MediaPipe Selfie Segmentation (if you want to stay with OpenCV)

For human portraits (e.g. profile photos), Google’s MediaPipe offers a machine-learning model to segment people from the background.

 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
import cv2
import mediapipe as mp
import numpy as np

# Load image
image = cv2.imread("your_photo.jpg")
image = cv2.resize(image, (640, 480))  # Resize to manageable dimensions
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Load MediaPipe Selfie Segmentation
mp_selfie_segmentation = mp.solutions.selfie_segmentation
segmentor = mp_selfie_segmentation.SelfieSegmentation(model_selection=1)

# Run segmentation
results = segmentor.process(image_rgb)
mask = results.segmentation_mask

# Create binary mask (thresholding)
condition = mask > 0.6
bg_color = (200, 200, 200)  # Replace background with gray
bg_image = np.ones(image.shape, dtype=np.uint8) * np.array(bg_color, dtype=np.uint8)

# Combine the images
output_image = np.where(condition[..., None], image, bg_image)

# Save result
cv2.imwrite("segmented_photo.jpg", output_image)
print("Saved with background replaced")

How to Remove the Background of an Image Using Python ?
How to Remove the Background of an Image Using Python ?

Tips for Mac Users (Avoiding MediaPipe Crashes in Jupyter Notebooks)

If you're running MediaPipe code on a Mac using Jupyter Notebook, you might encounter this frustrating error:

1
The kernel appears to have died. It will restart automatically.

Why This Happens

This issue usually occurs due to incompatibilities or resource issues, especially under the following conditions:

  • You're using Jupyter Notebook on a Mac (Intel or older ARM chips).
  • Your machine lacks AVX instruction set support (common in some virtualized or older Macs).
  • MediaPipe or its dependencies (like NumPy or TensorFlow) are misaligned with your environment.
  • The input image is too large, and MediaPipe tries to allocate too much memory.

The Solution: Use Google Colab Instead

A simple and effective workaround is to move your notebook and image to Google Drive and run the code via Google Colab, which provides a more compatible execution environment.

Step-by-Step Instructions

Step 1: Move Files to Google Drive

  • Upload your Jupyter Notebook (.ipynb) and the input image (your_photo.jpg) to your Google Drive.
  • Place them in a folder like: My Drive > Coding > Testing.

Step 2: Mount Google Drive in Colab

In the first code cell of your Colab notebook, run:

1
2
from google.colab import drive
drive.mount('/content/drive')

This will prompt you to authenticate and link your Google Drive to the Colab session.

Step 3: Clean and Reinstall MediaPipe (Important!)

Uninstall existing packages to avoid version conflicts:

1
2
!pip uninstall -y mediapipe keras pandas numpy
!pip install mediapipe

This ensures you're using a compatible and clean version of MediaPipe.

Step 4: Run the Background Removal Code

Paste and run the following code in a new cell:

 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
import cv2
import mediapipe as mp
import numpy as np

# Load the image from Google Drive
image_path = "/content/drive/My Drive/Coding/Testing/your_photo.jpg"  # Replace with your path
image = cv2.imread(image_path)
image = cv2.resize(image, (640, 480))  # Resize to avoid memory issues
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Initialize MediaPipe Selfie Segmentation
mp_selfie_segmentation = mp.solutions.selfie_segmentation
segmentor = mp_selfie_segmentation.SelfieSegmentation(model_selection=1)

# Run segmentation to get person mask
results = segmentor.process(image_rgb)
mask = results.segmentation_mask

# Create binary mask
condition = mask > 0.6  # Tune threshold if needed
bg_color = (200, 200, 200)  # Set background to gray
bg_image = np.ones(image.shape, dtype=np.uint8) * np.array(bg_color, dtype=np.uint8)

# Composite person over gray background
output_image = np.where(condition[..., None], image, bg_image)

# Save the output to your Colab environment
cv2.imwrite("segmented_photo.jpg", output_image)
print("Background replaced and saved as segmented_photo.jpg")

Important:

Replace the following line with the correct path to your image:

1
image_path = "/content/drive/My Drive/Coding/Testing/your_photo.jpg"

If your image is in a different folder, adjust the path accordingly.

Result

This will create a new file segmented_photo.jpg in your Colab environment (you can download it or save it back to Drive). The background will be replaced with gray, and the subject will remain visible and clean.

Method 3: Using remove.bg API

The remove.bg API provides an AI-powered background removal service.

Installation

1
pip install removebg

Code Implementation

1
2
3
4
5
6
7
from removebg import RemoveBg

api_key = "your_api_key_here"
rmbg = RemoveBg(api_key, "error.log")

# Remove background
rmbg.remove_background_from_img_file("input_image.jpg")

This method is cloud-based and requires an API key from remove.bg.

Conclusion

Method Best Use Case Requires Internet Output
rembg Profile photos, general use PNG with transparency
OpenCV Solid-colored backgrounds PNG (may need further cleanup)
remove.bg API Quick results via cloud PNG with transparency

Each method has its strengths, so choose based on your specific use case.

Image

of