Rembg
Rembg is an open-source Python library that uses deep learning models to automatically remove backgrounds from images. It provides a simple API and command-line interface, making it suitable for various applications such as e-commerce, graphic design, and automated content creation. The library is actively maintained, with frequent patch releases, and is currently at version 2.0.75.
Warnings
- gotcha First-time usage requires downloading AI models (100-300 MB each). This causes an initial delay and requires an active internet connection. Models are cached locally in `~/.u2net` for subsequent offline use.
- gotcha For optimal performance when processing multiple images, explicitly create and reuse a session using `new_session()` instead of relying on the default behavior, which initializes a new session for each call.
- gotcha GPU (NVIDIA/CUDA or AMD/ROCm) acceleration (`rembg[gpu]` or `rembg[rocm]`) requires specific system setups including `onnxruntime-gpu` or `onnxruntime-rocm`, potentially CUDA/cudnn-devel or ROCm libraries. Incorrect setup often leads to `ModuleNotFoundError` for `onnxruntime` or `DLL load failed` errors on Windows.
- breaking Python version compatibility has changed. Current `rembg` versions require Python `>=3.11, <3.14`. Older versions might have supported broader ranges (e.g., `>=3.7, <3.11`). Using an unsupported Python version will lead to installation failures or `ModuleNotFoundError`.
- gotcha Depending on the model and image size, `rembg` can be memory-intensive, especially with GPU acceleration. Large images or batch processing can lead to Out Of Memory (OOM) errors.
Install
-
pip install "rembg[cpu]" -
pip install "rembg[gpu]" -
pip install "rembg[rocm]" -
pip install "rembg[cpu,cli]"
Imports
- remove
from rembg import remove
- new_session
from rembg import new_session
- Image
from PIL import Image
Quickstart
from PIL import Image
from rembg import remove, new_session
# Create a session to improve performance for multiple images
session = new_session()
# Example: Load an image from a dummy source or local path
# For a real application, replace this with actual image loading
# For demonstration, we'll create a blank image
# In a real scenario, you'd use Image.open('path/to/your/image.png')
try:
# Simulate loading an image (replace with actual image path)
input_image = Image.new('RGBA', (200, 200), (255, 0, 0, 255)) # A red square
# Or, if you have an actual image file:
# input_image = Image.open('path/to/your/image.png')
# Remove the background
output_image = remove(input_image, session=session)
# Save the result
# output_image.save('output.png')
print("Background removed successfully (output not saved in this example).")
print(f"Original image mode: {input_image.mode}, size: {input_image.size}")
print(f"Output image mode: {output_image.mode}, size: {output_image.size}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure you have an actual image file or mock image data for processing.")