Skip to content

armgit / rembg

Public Imported from GitHub
0

Rembg is a tool to remove images background

Fix crash in `rembg i` when output path is omitted (#829)

Daniel Gatis98f3a9fa5397
diff --git a/README.md b/README.md
index a8fb640..ff8dd3e 100644
--- a/README.md
+++ b/README.md
@@ -128,6 +128,15 @@ curl -s http://input.png | rembg i > output.png
rembg i path/to/input.png path/to/output.png
```
+**Omit the output path** (writes `<input_stem>.out.png` next to the input):
+
+```shell
+rembg i path/to/input.png
+# → path/to/input.out.png
+```
+
+If `stdout` is redirected (e.g. `rembg i input.png > out.png`), the output is written to `stdout` instead.
+
**Specify a model:**
```shell
diff --git a/rembg/commands/i_command.py b/rembg/commands/i_command.py
index 8454142..b840e3d 100644
--- a/rembg/commands/i_command.py
+++ b/rembg/commands/i_command.py
@@ -1,4 +1,5 @@
import json
+import pathlib
import sys
from typing import IO
@@ -105,4 +106,11 @@ def i_command(model: str, extras: str, input: IO, output: IO, **kwargs) -> None:
except Exception:
pass
+ if output is None:
+ input_name = getattr(input, "name", "")
+ if input_name and input_name != "<stdin>" and sys.stdout.isatty():
+ output = pathlib.Path(input_name).with_suffix(".out.png").open("wb")
+ else:
+ output = click.get_binary_stream("stdout")
+
output.write(remove(input.read(), session=new_session(model, **kwargs), **kwargs))