Transparent Background Image/Video Remover
Transparent-background is a Python library that leverages InSPyReNet (ACCV 2022) to remove backgrounds from images and videos, making them transparent. It supports various output modes, including RGBA, green screen, blur, and overlay. The library is actively maintained, with frequent updates addressing performance, dependencies, and new features.
Common errors
-
ModuleNotFoundError: No module named 'flet'
cause The GUI dependency `flet` is no longer part of the default installation.fixInstall the library with the GUI extra: `pip install transparent-background[gui]`. -
Failed to download checkpoint file. Please check your internet connection or try again later.
cause Issues with downloading pre-trained model checkpoints, potentially due to `gdown` instability (older versions) or network problems.fixEnsure a stable internet connection. For versions >=1.2.12, make sure `config.yaml` is removed from `$HOME/.transparent_background`. If problems persist, manually download the `.pth` checkpoint file from the GitHub releases page and place it in `$HOME/.transparent_background`. -
ModuleNotFoundError: No module named 'pyvirtualcam'
cause Webcam functionality requires an optional dependency that is no longer installed by default.fixInstall the library with the webcam extra: `pip install transparent-background[webcam]`. -
AttributeError: 'Remover' object has no attribute 'process_video' (or similar for webcam, when using rgba)
cause The `rgba` output type is not supported for video or webcam inputs, as stated in the documentation.fixWhen processing video or webcam streams, choose a different output `type` such as `'green'`, `'blur'`, or `'overlay'`. For example, `remover.process(frame, type='green')`.
Warnings
- breaking The `flet` (GUI) dependency was moved to an extra (`[gui]`) due to import errors with `flet` versions >= 0.25.0.
- deprecated The `--jit` command-line option is deprecated. Its functionality (for stability) has been replaced by the `--resize` option.
- breaking The `gdown` module for checkpoint downloading was replaced by `wget` due to instability. This migration required users to manually remove the `config.yaml` file from `$HOME/.transparent_background`.
- deprecated The `--fast` command-line argument is deprecated and has been replaced by the `--mode` argument.
- gotcha The 'rgba' output type (generating an alpha map) is not supported for video or webcam input due to underlying technical limitations.
- gotcha The default configuration file directory (`~/.transparent-background/config.yaml`) might be inaccessible on non-PC systems.
Install
-
pip install transparent-background -
pip install transparent-background[gui] -
pip install transparent-background[webcam] -
pip install --extra-index-url https://download.pytorch.org/whl/cu118 transparent-background
Imports
- Remover
from transparent_background import Remover
- Image
import Image
from PIL import Image
Quickstart
import os
from PIL import Image
from transparent_background import Remover
# Create a dummy image for demonstration
try:
img = Image.new('RGB', (200, 200), color = 'red')
# Add a white circle in the middle
from PIL import ImageDraw
draw = ImageDraw.Draw(img)
draw.ellipse((50, 50, 150, 150), fill='white', outline='white')
input_image_path = 'input_image.png'
output_image_path = 'output_transparent_image.png'
img.save(input_image_path)
print(f"Dummy image saved to {input_image_path}")
# Initialize the Remover (downloads model checkpoint on first run)
# Consider setting TRANSPARENT_BACKGROUND_FILE_PATH if default home directory is an issue
remover = Remover(fast=False, jit=False, device='cpu') # 'cuda' for GPU if available
# Load an image
img = Image.open(input_image_path)
# Process the image to remove background
# 'bg_color=(0, 0, 0, 0)' ensures a fully transparent background for RGBA
out = remover.process(img, type='rgba', threshold=0.5, bg_color=(0,0,0,0))
# Save the output image
out.save(output_image_path)
print(f"Processed image saved to {output_image_path}")
# Clean up dummy image
os.remove(input_image_path)
os.remove(output_image_path)
print("Cleaned up dummy images.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure PyTorch is installed correctly for your system (e.g., with CUDA support if desired).")
print("You might also need to install optional dependencies like `flet` or `pyvirtualcam` if using GUI/webcam features.")