Removing the background from product photos or icon assets takes time when you select the outline point by point in an image editor. rembg is a Python library that uses the deep-learning model U2Net to identify the foreground and background in an image, then makes the background transparent while preserving the foreground. I installed it and tested background removal with the network disconnected.
rembg uses a salient object detection model called U2Net. It identifies the main subject in an image and replaces the background with a transparent alpha channel. The rembg project uses the MIT License (*1).
1. Install it
FROM docker.io/library/python:3.12.13-slim
RUN apt-get update && apt-get install -y --no-install-recommends libgl1 libglib2.0-0 && rm -rf /var/lib/apt/lists/*
RUN pip install --no-cache-dir --quiet 'rembg[cpu]==2.0.76'
COPY warmup_model.py /tmp/warmup_model.py
RUN python /tmp/warmup_model.py
WORKDIR /work
# warmup_model.py (download the U2Net model during the build)
from rembg import new_session
new_session("u2net")
print("model downloaded ok")
podman build -t rembg-sandbox .
The base rembg package does not include the inference engine (onnxruntime), so you need to install the CPU inference extra as rembg[cpu].
2. Remove the background
I generated a composite image of simple shapes in the test environment, with a blue background and a yellow circle, and passed it to rembg without modification. The test used no external image. I ran it with the network disconnected.
from PIL import Image, ImageDraw
from rembg import remove
img = Image.new("RGB", (400, 400), color=(40, 120, 200))
draw = ImageDraw.Draw(img)
draw.ellipse((100, 100, 300, 300), fill=(240, 200, 60))
img.save("/work/input.png")
with open("/work/input.png", "rb") as f:
output_bytes = remove(f.read())
with open("/work/output.png", "wb") as f:
f.write(output_bytes)
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-v "$(pwd)":/work -w /work rembg-sandbox \
python remove_bg_demo.py
The -v option makes the specified directory available for reads and writes inside the container. Run this in an empty directory reserved for the experiment.
To check that rembg removed the background, I composited the output image, a transparent PNG, onto a red background and compared it with the original blue background.
The background color changed across the entire image. U2Net identified the boundary between the foreground (the circle) and the background (the colored field) in this simple composite image, then made the background transparent while preserving the circle. The transparent region covered 79.2% of the full image. The circle covered about 19.6%, so subtracting it from the whole gives the same figure.
Letting a Machine Decide the Boundary
Removing a background requires you to decide where the subject ends as you select its outline. rembg lets the model make that decision. Since U2Net separated the simple shape from its background, you can expect the same mechanism to separate the background from a real photo.
Sources
*1 rembg (GitHub), https://github.com/danielgatis/rembg, MIT License