# rembg

Видаляє фон за об’єктом.  Використовує навчені моделі (посилання є на GitHub)

<https://pypi.org/project/rembg/2.0.28/>

```
pip install rembg
```

{% embed url="<https://github.com/danielgatis/rembg>" %}

Встановлювати в вірт.середоище. Ставить близько 700 Мб модулів.

```
attrs
certifi
charset-normalizer
coloredlogs
flatbuffers
humanfriendly
idna
imageio
jsonschema
jsonschema-specifications
lazy_loader
llvmlite
mpmath
networkx
numba
numpy
onnxruntime
opencv-python-headless
packaging
pillow
platformdirs
pooch
protobuf
pymatting
referencing
requests
rpds-py
scikit-image
scipy
sympy
tifffile
tqdm
urllib3
```

```python
#!/home/user/dev/rembg/env/bin/python

from rembg import remove

input_path = '/home/user/dev/rembg/test.jpg'
output_path = '/home/user/dev/rembg/test_output.png'

with open(input_path, 'rb') as i:
    with open(output_path, 'wb') as o:
        input = i.read()
        output = remove(input)
        o.write(output)

```

При першому запуску становлює файл загальної навченої моделі в `/home/user/.u2net/`

В цю директорію можно скопіювати й інші моделі.

Щоб запускати з командної строки необхідно ще наставити модулів:

```
pip install click
pip install filetype
pip install watchdog
pip install aiohttp
    > multidict, frozenlist, async-timeout, yarl, aiosignal, aiohttp
pip install gradio
    > aiofiles, altair, annotated-types, anyio, contourpy, cycler, exceptiongroup,
    fastapi, ffmpy, filelock, fonttools, fsspec, gradio-client, h11, httpcore, httpx,
    huggingface-hub, importlib-resources, jinja2, kiwisolver, markdown-it-py,
    markupsafe, matplotlib, mdurl, orjson, pandas, pydantic, pydantic-core, pydub,
    pygments, pyparsing, python-dateutil, python-multipart, pytz, pyyaml, rich, ruff,
    semantic-version, shellingham, six, sniffio, starlette, tomlkit, toolz, typer,
    typing-extensions, tzdata, uvicorn, websockets
pip install asyncer
```

Тепер можна запускати так:

```
rembg i -m MODEL ~/img/test.jpg ~/img/test_output.pngg
```

де MODEL - необхідний файл моделі.

Трохи краще результат, з ключем `-a` - виходить менша сіра кайма по краям:

```
rembg i -a -m MODEL ~/img/test.jpg ~/img/test_output.png
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://olexsyn.gitbook.io/enote/progr/python/modules/rembg.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
