background-remove 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +317 -0
- package/bin/cli.js +561 -0
- package/package.json +52 -0
- package/src/index.js +778 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Richard Anaya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# background-remove
|
|
2
|
+
|
|
3
|
+
**An AI-first CLI tool for removing backgrounds from images.**
|
|
4
|
+
|
|
5
|
+
Built specifically for AI assistants, agents, and automated workflows. Every feature is designed for programmatic use: extensive help text, detailed validation with corrective suggestions, and predictable error handling. Also works great for humans!
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Works out of the box
|
|
9
|
+
npx background-remove remove photo.jpg
|
|
10
|
+
|
|
11
|
+
# Or install globally
|
|
12
|
+
npm install -g background-remove
|
|
13
|
+
background-remove remove photo.jpg output.png --method inferred --tolerance 40
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Why AI-First?
|
|
17
|
+
|
|
18
|
+
| Feature | Purpose |
|
|
19
|
+
|---------|---------|
|
|
20
|
+
| **Extensive `--help`** | Every command has detailed descriptions with ranges, formats, and examples |
|
|
21
|
+
| **Smart validation** | Invalid inputs return specific suggestions for correction |
|
|
22
|
+
| **`methods` command** | Self-documenting guide for choosing the right algorithm |
|
|
23
|
+
| **`preview` command** | Test parameters before committing to final output |
|
|
24
|
+
| **Consistent errors** | All errors include actionable next steps |
|
|
25
|
+
| **Verbose mode** | See exactly what the tool is doing and why |
|
|
26
|
+
|
|
27
|
+
## Quick Reference for AI Assistants
|
|
28
|
+
|
|
29
|
+
**Most common commands:**
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# 1. Auto-remove (works for 80% of images)
|
|
33
|
+
background-remove remove input.jpg output.png
|
|
34
|
+
|
|
35
|
+
# 2. White background removal
|
|
36
|
+
background-remove remove input.png --method color --color white --tolerance 30
|
|
37
|
+
|
|
38
|
+
# 3. Green screen
|
|
39
|
+
background-remove remove input.jpg --method chroma --chroma-color green --tolerance 50 --smooth
|
|
40
|
+
|
|
41
|
+
# 4. Preview before processing
|
|
42
|
+
background-remove preview input.jpg --method color --color white
|
|
43
|
+
background-remove remove input.jpg --method color --color white --tolerance 35
|
|
44
|
+
|
|
45
|
+
# 5. Get help on any command
|
|
46
|
+
background-remove remove --help
|
|
47
|
+
background-remove methods
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**When things go wrong:**
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# See all error suggestions
|
|
54
|
+
background-remove remove bad-input.jpg --method invalid --tolerance 999
|
|
55
|
+
|
|
56
|
+
# Check what methods are available
|
|
57
|
+
background-remove methods
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Installation
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npm install -g background-remove
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Or use without installing:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
npx background-remove remove photo.jpg
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Commands
|
|
73
|
+
|
|
74
|
+
### `remove <input> [output]`
|
|
75
|
+
|
|
76
|
+
Remove background from an image and save as PNG/WebP with transparency.
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
background-remove remove photo.jpg # Creates photo-nobg.png
|
|
80
|
+
background-remove remove photo.jpg output.png # Specify output
|
|
81
|
+
background-remove remove photo.jpg output.webp --format webp --quality 85
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Arguments:**
|
|
85
|
+
- `input` - Path to input image (JPG, PNG, WebP, GIF, TIFF, AVIF)
|
|
86
|
+
- `output` - (Optional) Output path. Defaults to `<input>-nobg.<format>`
|
|
87
|
+
|
|
88
|
+
**Options:**
|
|
89
|
+
|
|
90
|
+
| Option | Description | Default |
|
|
91
|
+
|--------|-------------|---------|
|
|
92
|
+
| `-m, --method <method>` | Removal algorithm: `auto`, `color`, `inferred`, `chroma`, `flood`, `edges` | `auto` |
|
|
93
|
+
| `-c, --color <color>` | Color to remove (hex #FFF, rgb(), or named) | `#FFFFFF` |
|
|
94
|
+
| `-t, --tolerance <number>` | Color matching tolerance 0-255 | `32` |
|
|
95
|
+
| `--chroma-color <color>` | Chroma key: `green`, `blue`, `red`, or hex | `green` |
|
|
96
|
+
| `--flood-seed <positions...>` | Seed points as "x,y" pairs | `0,0` |
|
|
97
|
+
| `-r, --radius <number>` | Corner sampling radius 1-100 | `10` |
|
|
98
|
+
| `-d, --distance <number>` | Edge detection threshold 1-50 | `10` |
|
|
99
|
+
| `-f, --feather <number>` | Edge feathering 0-20 | `0` |
|
|
100
|
+
| `-s, --smooth` | Apply edge smoothing | `false` |
|
|
101
|
+
| `-a, --antialias` | Apply antialiasing | `false` |
|
|
102
|
+
| `-i, --invert` | Keep background, remove foreground | `false` |
|
|
103
|
+
| `--format <format>` | Output: `png`, `webp`, `jpeg` | `png` |
|
|
104
|
+
| `-q, --quality <number>` | WebP/JPEG quality 1-100 | `90` |
|
|
105
|
+
| `-v, --verbose` | Detailed processing information | `false` |
|
|
106
|
+
|
|
107
|
+
Run `background-remove remove --help` for full documentation with examples.
|
|
108
|
+
|
|
109
|
+
### `methods`
|
|
110
|
+
|
|
111
|
+
Display detailed information about all removal algorithms with use cases and examples.
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
background-remove methods
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Output includes:
|
|
118
|
+
- What each method does
|
|
119
|
+
- When to use it ("Best for:")
|
|
120
|
+
- All available options
|
|
121
|
+
- Working command examples
|
|
122
|
+
|
|
123
|
+
### `preview <input>`
|
|
124
|
+
|
|
125
|
+
Generate a mask preview before processing. White = kept, Black = removed.
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
background-remove preview photo.jpg --method color --color white
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Use this to tune `--tolerance` before running `remove`.
|
|
132
|
+
|
|
133
|
+
## Removal Methods
|
|
134
|
+
|
|
135
|
+
### auto (default)
|
|
136
|
+
Automatically selects the best method. Uses inferred detection internally. Best starting point for any image.
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
background-remove remove photo.jpg --method auto --tolerance 40
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### color
|
|
143
|
+
Remove a specific exact color. Most reliable when you know the precise background color.
|
|
144
|
+
|
|
145
|
+
**Best for:** Solid color backgrounds with known color, white/black backgrounds, brand-colored backgrounds, screenshots, logos.
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
# Remove white
|
|
149
|
+
background-remove remove logo.png --method color --color white --tolerance 30
|
|
150
|
+
|
|
151
|
+
# Remove hex color
|
|
152
|
+
background-remove remove photo.png --method color --color "#FF5733" --tolerance 40
|
|
153
|
+
|
|
154
|
+
# RGB format
|
|
155
|
+
background-remove remove image.png --method color --color "rgb(255,87,51)"
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### inferred
|
|
159
|
+
Auto-detects background by sampling corners/edges. Assumes uniform background at image boundaries.
|
|
160
|
+
|
|
161
|
+
**Best for:** Product photos with white backgrounds, portraits with plain walls, centered subjects.
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
background-remove remove product.jpg --method inferred --tolerance 40 --radius 15
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### chroma
|
|
168
|
+
Chroma key / green screen removal.
|
|
169
|
+
|
|
170
|
+
**Best for:** Green screen photography, blue screen video frames, studio chroma key shots.
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
background-remove remove video.jpg --method chroma --chroma-color green --tolerance 50
|
|
174
|
+
background-remove remove video.jpg --method chroma --chroma-color blue --smooth
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### flood
|
|
178
|
+
Flood fill from seed points, removes connected similar-color regions.
|
|
179
|
+
|
|
180
|
+
**Best for:** Diagrams, charts, product photos with connected backgrounds.
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
background-remove remove diagram.png --method flood --flood-seed 0,0
|
|
184
|
+
background-remove remove diagram.png --method flood --flood-seed 0,0 100,50 200,100
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### edges
|
|
188
|
+
Gradient-based edge detection. Removes areas outside detected boundaries.
|
|
189
|
+
|
|
190
|
+
**Best for:** High-contrast subjects, objects with clear boundaries, silhouettes.
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
background-remove remove object.jpg --method edges --distance 15 --tolerance 35
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Smart Validation & Error Recovery
|
|
197
|
+
|
|
198
|
+
The CLI validates all inputs and provides specific suggestions for fixes:
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
$ background-remove remove photo.jpg --method invalid --tolerance 300 --color badcolor
|
|
202
|
+
|
|
203
|
+
✗ Validation errors found:
|
|
204
|
+
• Invalid method "invalid"
|
|
205
|
+
• Tolerance must be between 0-255, got: 300
|
|
206
|
+
• Invalid color format: "badcolor"
|
|
207
|
+
|
|
208
|
+
💡 Suggestions:
|
|
209
|
+
• Valid methods are: auto, color, inferred, chroma, flood, edges
|
|
210
|
+
• Run 'background-remove methods' to see all methods with descriptions
|
|
211
|
+
• Low values (0-30): Remove only very similar colors
|
|
212
|
+
• Medium values (30-60): Good for solid backgrounds
|
|
213
|
+
• High values (60-255): Remove more varied colors
|
|
214
|
+
• Use hex format: #FFFFFF or #FFF
|
|
215
|
+
• Use RGB format: rgb(255,255,255)
|
|
216
|
+
• Use named colors: white, black, red, green, blue, yellow, cyan, magenta, gray
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Color Formats
|
|
220
|
+
|
|
221
|
+
| Format | Example |
|
|
222
|
+
|--------|---------|
|
|
223
|
+
| Hex (long) | `#FFFFFF` |
|
|
224
|
+
| Hex (short) | `#FFF` |
|
|
225
|
+
| RGB | `rgb(255,255,255)` |
|
|
226
|
+
| Named | `white`, `black`, `red`, `green`, `blue`, `yellow`, `cyan`, `magenta`, `gray` |
|
|
227
|
+
|
|
228
|
+
## Tolerance Guide
|
|
229
|
+
|
|
230
|
+
| Range | Use Case |
|
|
231
|
+
|-------|----------|
|
|
232
|
+
| 10-20 | Exact matches only |
|
|
233
|
+
| 30-40 | Solid backgrounds (default: 32) |
|
|
234
|
+
| 60-80 | Gradients, shadows, variations |
|
|
235
|
+
| 100+ | Very aggressive |
|
|
236
|
+
|
|
237
|
+
## Output Formats
|
|
238
|
+
|
|
239
|
+
| Format | Transparency | Best For |
|
|
240
|
+
|--------|--------------|----------|
|
|
241
|
+
| PNG | Yes | Maximum compatibility |
|
|
242
|
+
| WebP | Yes | Web use, smaller files |
|
|
243
|
+
| JPEG | No | Fills transparent areas with white |
|
|
244
|
+
|
|
245
|
+
## Method Selection Guide
|
|
246
|
+
|
|
247
|
+
| Image Type | Method |
|
|
248
|
+
|------------|--------|
|
|
249
|
+
| Unknown/Mixed | `auto` |
|
|
250
|
+
| White background | `color` or `inferred` |
|
|
251
|
+
| Green/Blue screen | `chroma` |
|
|
252
|
+
| Product photo | `inferred` |
|
|
253
|
+
| Diagram/Chart | `flood` |
|
|
254
|
+
| High contrast | `edges` |
|
|
255
|
+
| Known hex color | `color` |
|
|
256
|
+
|
|
257
|
+
## Programmatic Usage
|
|
258
|
+
|
|
259
|
+
Use from Node.js code:
|
|
260
|
+
|
|
261
|
+
```javascript
|
|
262
|
+
const { removeBackground } = require('background-remove');
|
|
263
|
+
|
|
264
|
+
const result = await removeBackground('input.jpg', 'output.png', {
|
|
265
|
+
method: 'inferred',
|
|
266
|
+
tolerance: 40,
|
|
267
|
+
feather: 2,
|
|
268
|
+
smooth: true
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
console.log(result.outputPath); // /path/to/output.png
|
|
272
|
+
console.log(result.detectedColor); // [255, 255, 255]
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Examples for AI Assistants
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
# Basic removal
|
|
279
|
+
background-remove remove input.jpg output.png
|
|
280
|
+
|
|
281
|
+
# White background with validation
|
|
282
|
+
background-remove remove photo.jpg --method color --color white --tolerance 30
|
|
283
|
+
|
|
284
|
+
# Green screen with smoothing
|
|
285
|
+
background-remove remove video.jpg --method chroma --chroma-color green --tolerance 60 --smooth
|
|
286
|
+
|
|
287
|
+
# Auto-detect with feathered edges
|
|
288
|
+
background-remove remove product.jpg --method inferred --tolerance 40 --feather 2
|
|
289
|
+
|
|
290
|
+
# Multiple flood seeds
|
|
291
|
+
background-remove remove diagram.png --method flood --flood-seed 0,0 800,0 0,600 800,600
|
|
292
|
+
|
|
293
|
+
# Edge detection
|
|
294
|
+
background-remove remove subject.jpg --method edges --distance 12 --tolerance 30
|
|
295
|
+
|
|
296
|
+
# Preview then process
|
|
297
|
+
background-remove preview photo.jpg --method color --color "#FF5733"
|
|
298
|
+
background-remove remove photo.jpg --method color --color "#FF5733" --tolerance 35
|
|
299
|
+
|
|
300
|
+
# WebP output
|
|
301
|
+
background-remove remove photo.jpg output.webp --method auto --format webp --quality 85
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## Troubleshooting
|
|
305
|
+
|
|
306
|
+
| Problem | Solution |
|
|
307
|
+
|---------|----------|
|
|
308
|
+
| Too much removed | Lower `--tolerance` |
|
|
309
|
+
| Not enough removed | Raise `--tolerance` |
|
|
310
|
+
| Jagged edges | Use `--smooth` or `--antialias` |
|
|
311
|
+
| Hard edges | Use `--feather 3` or higher |
|
|
312
|
+
| Green screen issues | Increase `--tolerance` to 60+ |
|
|
313
|
+
| Shadows remain | Try `--method inferred` with higher tolerance |
|
|
314
|
+
|
|
315
|
+
## License
|
|
316
|
+
|
|
317
|
+
MIT - Copyright (c) 2024 Richard Anaya
|