rembg

repository·main·Indexed 12 days ago

https://github.com/danielgatis/rembg

A versatile tool for removing image backgrounds, available as a Python library, CLI, HTTP server, or Docker container. It supports various hardware backends including CPU, NVIDIA CUDA, and AMD ROCm, and provides a wide range of pre-trained ONNX models for general, human, cloth, and anime segmentation.

Tokens
8.2K
Snippets
43
Records
49
Agent score
48%

What's inside rembg

  1. Use custom model files

    main

    If you have a modified version of a model (e.g. converted to a different ONNX IR version for compatibility with an older CUDA toolkit), you can prevent rembg from overwriting it by following these steps:

    1. Set MODEL_CHECKSUM_DISABLED=1.
    2. Place your custom .onnx file in the models directory (default ~/.u2net/) with the expected filename (e.g. u2net.onnx).
    3. Rembg will detect the existing file and use it instead of re-downloading the original.
    export MODEL_CHECKSUM_DISABLED=1
    # Place custom u2net.onnx in ~/.u2net/
    rembg i input.png
  2. Basic background removal with remove()

    main

    To remove a background from an image, use the remove() function. By default, it uses the u2net model. You must provide a PIL Image object as the input.

    from PIL import Image
    from rembg import remove
    
    input_path = 'input.png'
    output_path = 'output.png'
    
    input_img = Image.open(input_path)
    output = remove(input_img)
    output.save(output_path)
  3. Use the withoutbg cloud API

    main

    The withoutbg model is a cloud API backend. Images sent to this backend have a maximum upload size of 20 MB. To use it, you must provide an API key via the -x CLI flag, the api_key parameter in new_session(...), or by setting the WITHOUTBG_API_KEY environment variable.

    # Example using environment variable
    export WITHOUTBG_API_KEY='your_api_key_here'
    rembg i input.png
  4. Optimize multi-image processing with sessions

    main

    Calling remove() without a session causes a new session to be initialized every time, which is a significant performance bottleneck. For processing multiple images, initialize a single session once and reuse it for every call.

    model_name = "unet"
    rembg_session = new_session(model_name)
    for img in images:
        output = remove(img, session=rembg_session)
  5. Install rembg

    main

    Install rembg using pip. Choose the backend that matches your hardware:

    CPU

    pip install "rembg[cpu,cli]" # for library + cli

    NVIDIA GPU (CUDA)

    Ensure your system is compatible with onnxruntime-gpu. NVIDIA GPUs may require onnxruntime-gpu, CUDA, and cudnn-devel.

    pip install "rembg[gpu,cli]" # for library + cli

    AMD GPU (ROCm)

    Requires onnxruntime-rocm to be installed following AMD's documentation.

    pip install "rembg[rocm,cli]" # for library + cli

    Requirements: Python >=3.11, <3.14.

    pip install "rembg[cpu,cli]"
  6. Use rembg with Docker

    main

    Run rembg in a containerized environment.

    CPU Only

    docker run -v .:/data danielgatis/rembg i /data/input.png /data/output.png

    NVIDIA CUDA GPU

    Requires the NVIDIA Container Toolkit. Because CUDA acceleration requires cudnn-devel, you must build the image locally.

    1. Build: docker build -t rembg-nvidia-cuda-cudnn-gpu -f Dockerfile_nvidia_cuda_cudnn_gpu .
    2. Run: sudo docker run --rm -it --gpus all -v /dev/dri:/dev/dri -v $PWD:/data rembg-nvidia-cuda-cudnn-gpu i -m birefnet-general /data/input.png /data/output.png

    Tip: Use -v /path/to/models/:/root/.u2net to persist model files and avoid re-downloads.

    docker run -v .:/data danielgatis/rembg i /data/input.png /data/output.png
  7. Install rembg with CLI support

    main

    To use the rembg command-line interface, you must install the package with the cli extra. Depending on your hardware, use one of the following commands:

    • For CPU support: pip install "rembg[cpu,cli]"
    • For NVIDIA/CUDA GPU support: pip install "rembg[gpu,cli]"
    pip install "rembg[cpu,cli]"  # for CPU
    pip install "rembg[gpu,cli]"  # for NVIDIA/CUDA GPU
  8. Replace background with a specific color

    main

    Use the bgcolor argument to replace the removed background with a specific color. The argument accepts a tuple representing the color (e.g., RGBA).

    # Example: Replace background with white
    output = remove(input_img, bgcolor=(255, 255, 255, 255))
  9. Use a specific model with new_session()

    main

    To use a model other than the default u2net, use new_session(model_name) to create a session and pass it to the remove() function via the session argument.

    from rembg import new_session, remove
    
    model_name = "isnet-general-use"
    session = new_session(model_name)
    output = remove(input_img, session=session)