Skip to content

armgit / rembg

Public Imported from GitHub
0

Rembg is a tool to remove images background

better error msgs

Daniel Gatis6db7b4abef6f
diff --git a/README.md b/README.md
index 2a07dd6..9236bfa 100644
--- a/README.md
+++ b/README.md
@@ -41,23 +41,16 @@ python: >=3.11, <3.14
## Installation
-If you have `onnxruntime` already installed, just install `rembg`:
+Choose **one** of the following backends based on your hardware:
-```bash
-pip install rembg # for library
-pip install "rembg[cli]" # for library + cli
-```
-
-Otherwise, install `rembg` with explicit CPU/GPU support.
-
-### CPU support:
+### CPU support
```bash
-pip install rembg[cpu] # for library
+pip install "rembg[cpu]" # for library
pip install "rembg[cpu,cli]" # for library + cli
```
-### GPU support (NVidia/Cuda):
+### GPU support (NVIDIA/CUDA)
First of all, you need to check if your system supports the `onnxruntime-gpu`.
diff --git a/rembg/bg.py b/rembg/bg.py
index c8c752e..d878287 100644
--- a/rembg/bg.py
+++ b/rembg/bg.py
@@ -4,7 +4,21 @@ from enum import Enum
from typing import Any, List, Optional, Tuple, Union, cast
import numpy as np
-import onnxruntime as ort
+
+try:
+ import onnxruntime as ort # type: ignore[import-untyped]
+except ImportError:
+ print("No onnxruntime backend found.")
+ print("Please install rembg with CPU or GPU support:")
+ print()
+ print(' pip install "rembg[cpu]" # for CPU')
+ print(' pip install "rembg[gpu]" # for NVIDIA/CUDA GPU')
+ print()
+ print(
+ "For more information, see: https://github.com/danielgatis/rembg#installation"
+ )
+ sys.exit(1)
+
from PIL import Image, ImageOps
from PIL.Image import Image as PILImage
from pymatting.alpha.estimate_alpha_cf import estimate_alpha_cf
diff --git a/rembg/cli.py b/rembg/cli.py
index 8625ab5..dc1e747 100644
--- a/rembg/cli.py
+++ b/rembg/cli.py
@@ -1,4 +1,25 @@
-import click
+import sys
+
+# Fast path for --version (avoid importing heavy dependencies)
+if len(sys.argv) == 2 and sys.argv[1] in ("--version", "-V"):
+ from importlib.metadata import version
+
+ print(f"rembg, version {version('rembg')}")
+ sys.exit(0)
+
+try:
+ import click
+except ImportError:
+ print("The CLI dependencies are not installed.")
+ print("Please install rembg with CLI support:")
+ print()
+ print(' pip install "rembg[cpu,cli]" # for CPU')
+ print(' pip install "rembg[gpu,cli]" # for NVIDIA/CUDA GPU')
+ print()
+ print(
+ "For more information, see: https://github.com/danielgatis/rembg#installation"
+ )
+ sys.exit(1)
from . import __version__
from .commands import command_functions