{"id":4235,"library":"rembg","title":"Rembg","description":"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.","status":"active","version":"2.0.75","language":"en","source_language":"en","source_url":"https://github.com/danielgatis/rembg","tags":["image processing","background removal","computer vision","AI","machine learning","deep learning"],"install":[{"cmd":"pip install \"rembg[cpu]\"","lang":"bash","label":"CPU support (recommended)"},{"cmd":"pip install \"rembg[gpu]\"","lang":"bash","label":"NVIDIA/CUDA GPU support"},{"cmd":"pip install \"rembg[rocm]\"","lang":"bash","label":"AMD/ROCm GPU support"},{"cmd":"pip install \"rembg[cpu,cli]\"","lang":"bash","label":"CPU with CLI (optional)"}],"dependencies":[{"reason":"Core inference engine for CPU. Included with `[cpu]` extra.","package":"onnxruntime","optional":true},{"reason":"Core inference engine for NVIDIA/CUDA GPUs. Included with `[gpu]` extra. Requires CUDA and cudnn-devel setup.","package":"onnxruntime-gpu","optional":true},{"reason":"Core inference engine for AMD/ROCm GPUs. Included with `[rocm]` extra. Requires ROCm setup.","package":"onnxruntime-rocm","optional":true},{"reason":"Image manipulation library, commonly used for input/output.","package":"pillow","optional":false},{"reason":"Numerical computing library, essential for image data handling.","package":"numpy","optional":false},{"reason":"Scientific computing library, used for various image processing tasks.","package":"scipy","optional":false},{"reason":"Used for alpha matting post-processing.","package":"pymatting","optional":false},{"reason":"Collection of algorithms for image processing.","package":"scikit-image","optional":false}],"imports":[{"note":"The primary function to remove backgrounds.","symbol":"remove","correct":"from rembg import remove"},{"note":"Creates an inference session for a specific model. Important for performance when processing multiple images.","symbol":"new_session","correct":"from rembg import new_session"},{"note":"Often used with `rembg` for opening and saving images.","symbol":"Image","correct":"from PIL import Image"}],"quickstart":{"code":"from PIL import Image\nfrom rembg import remove, new_session\n\n# Create a session to improve performance for multiple images\nsession = new_session()\n\n# Example: Load an image from a dummy source or local path\n# For a real application, replace this with actual image loading\n# For demonstration, we'll create a blank image\n# In a real scenario, you'd use Image.open('path/to/your/image.png')\n\ntry:\n    # Simulate loading an image (replace with actual image path)\n    input_image = Image.new('RGBA', (200, 200), (255, 0, 0, 255)) # A red square\n    # Or, if you have an actual image file:\n    # input_image = Image.open('path/to/your/image.png')\n\n    # Remove the background\n    output_image = remove(input_image, session=session)\n\n    # Save the result\n    # output_image.save('output.png')\n    print(\"Background removed successfully (output not saved in this example).\")\n    print(f\"Original image mode: {input_image.mode}, size: {input_image.size}\")\n    print(f\"Output image mode: {output_image.mode}, size: {output_image.size}\")\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Please ensure you have an actual image file or mock image data for processing.\")\n","lang":"python","description":"This quickstart demonstrates how to use `rembg` to remove the background from an image. It highlights the use of `new_session` for efficiency and assumes `PIL` for image handling. For a real scenario, replace the dummy image creation with loading from a file (e.g., `Image.open('input.png')`)."},"warnings":[{"fix":"Ensure an internet connection for the first run. Be aware of the initial delay for model download.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Initialize a session once with `session = new_session()` and pass it to subsequent `remove()` calls: `remove(image, session=session)`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Verify `onnxruntime` compatibility at `onnxruntime.ai`. On Windows, install the latest Visual C++ Redistributable. For NVIDIA, ensure CUDA and cudnn-devel are correctly installed. If issues persist, revert to `rembg[cpu]`.","message":"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.","severity":"gotcha","affected_versions":"All versions with GPU extras"},{"fix":"Use Python 3.11 or 3.12. Check the PyPI page for the exact `requires_python` range for your target `rembg` version.","message":"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`.","severity":"breaking","affected_versions":"Before 2.0.68, and any version outside of `>=3.11, <3.14`"},{"fix":"Consider processing images in smaller batches, resizing large images before processing, or using a less memory-intensive model (e.g., `u2netp`). Monitor system memory during operation.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}