---
name: remove-background
description: Remove the background from a local image with rembg using a repo-local Python environment. Use when the user wants a transparent PNG cutout, sticker-style subject isolation, product-photo cleanup, or a reusable local workflow for background removal. Handles bootstrap of rembg, model download, and local CLI usage.
---

# Remove Background

## Overview

Use `scripts/remove-background.sh` to remove the background from a local image with `rembg`.

In this repository:

- prefer source images under `assets/`
- save cutout outputs under `assets/` by default
- prefer PNG outputs when transparency is required

This skill is the narrow path for requests such as:

- `убери фон`
- `сделай прозрачный PNG`
- `вырежи объект`
- product-photo background cleanup
- quick subject cutout for later montage or design work

If the source image is still remote, first fetch it locally with [download-images/SKILL.md](../download-images/SKILL.md), then run this skill on the downloaded file.
This is the normal cleanup step for downloaded still images when they arrive with an unwanted flat background and need to become reusable overlay assets for the edit.

## Bootstrap Behavior

The wrapper script is designed to be mechanical and repeatable. It will:

1. Prefer the repo-local virtual environment at `.venv-imagegen/`
2. Create that virtual environment if it does not exist
3. Run `ensurepip` if the venv has no `pip`
4. Install `rembg[cpu,cli]` if `rembg` is not already available
5. Ensure the requested model exists under `~/.u2net/`
6. Run the actual background-removal command

For known models such as `u2net` and `u2netp`, the wrapper can prefetch the `.onnx` file directly with `curl`. This avoids the GitHub SSL EOF problem that sometimes happens through Python `requests` on this machine.

## Quick Start

Default high-quality model:

```bash
bash remove-background/scripts/remove-background.sh \
  assets/input.jpg \
  assets/input.nobg.png
```

Install/bootstrap only:

```bash
bash remove-background/scripts/remove-background.sh --install-only
```

Faster lightweight model:

```bash
bash remove-background/scripts/remove-background.sh \
  --model u2netp \
  assets/input.jpg \
  assets/input.fast.nobg.png
```

## Workflow

1. Normalize the request into:
   - input image path
   - output path, usually a `.png`
   - whether quality or speed matters more
2. If the image is still remote, download it into `assets/` first with [download-images/SKILL.md](../download-images/SKILL.md).
3. Use `u2net` by default for better general quality.
4. Use `u2netp` when the user wants a lighter first pass or the model download must stay small.
5. Save the result under `assets/` unless the user explicitly requests another destination.
6. Verify the output is a transparent PNG when transparency is the goal.

For downloaded still assets, prefer getting the useful image first and then running this cutout step when transparency is actually needed for the edit.

## Model Guidance

- `u2net`: default and higher quality for general use. First download is about `176 MB`.
- `u2netp`: much smaller and faster to get started. First download is about `4.6 MB`.

If the user does not ask for a specific model, prefer `u2net`.

## Verification

Useful checks after rendering:

```bash
file assets/input.nobg.png
identify assets/input.nobg.png
identify -format 'channels=%[channels]\nopaque=%[opaque]\n' assets/input.nobg.png
```

Expected outcome for a transparent cutout:

- PNG output
- `RGBA` or `srgba` channels
- `opaque=false`

## Troubleshooting

- First use is slower because the model may need to be downloaded.
- If the wrapper needs to bootstrap `rembg`, installation can take a while because upstream CLI extras pull in web/server dependencies too.
- If the default Python downloader fails with SSL EOF while pulling a model from GitHub, rerun the wrapper. For `u2net` and `u2netp`, it falls back to direct `curl` download with retries.
- If you only need a transparent cutout, do not export to JPEG because JPEG cannot preserve alpha.

## Resources

- `scripts/remove-background.sh`: repo-local wrapper around `rembg` bootstrap and execution
