remove-background-ai 0.1.0__tar.gz
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.
- remove_background_ai-0.1.0/LICENSE +201 -0
- remove_background_ai-0.1.0/MANIFEST.in +1 -0
- remove_background_ai-0.1.0/PKG-INFO +293 -0
- remove_background_ai-0.1.0/README.md +267 -0
- remove_background_ai-0.1.0/pyproject.toml +59 -0
- remove_background_ai-0.1.0/setup.cfg +4 -0
- remove_background_ai-0.1.0/src/remove_background_ai/__init__.py +28 -0
- remove_background_ai-0.1.0/src/remove_background_ai/__main__.py +3 -0
- remove_background_ai-0.1.0/src/remove_background_ai/cli.py +245 -0
- remove_background_ai-0.1.0/src/remove_background_ai/core.py +83 -0
- remove_background_ai-0.1.0/src/remove_background_ai/editor.py +76 -0
- remove_background_ai-0.1.0/src/remove_background_ai/errors.py +10 -0
- remove_background_ai-0.1.0/src/remove_background_ai/imaging.py +110 -0
- remove_background_ai-0.1.0/src/remove_background_ai/models.py +131 -0
- remove_background_ai-0.1.0/src/remove_background_ai/py.typed +0 -0
- remove_background_ai-0.1.0/src/remove_background_ai/sizing.py +56 -0
- remove_background_ai-0.1.0/src/remove_background_ai.egg-info/PKG-INFO +293 -0
- remove_background_ai-0.1.0/src/remove_background_ai.egg-info/SOURCES.txt +20 -0
- remove_background_ai-0.1.0/src/remove_background_ai.egg-info/dependency_links.txt +1 -0
- remove_background_ai-0.1.0/src/remove_background_ai.egg-info/entry_points.txt +2 -0
- remove_background_ai-0.1.0/src/remove_background_ai.egg-info/requires.txt +4 -0
- remove_background_ai-0.1.0/src/remove_background_ai.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Nick Powell
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prune tests
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: remove-background-ai
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Remove image backgrounds with OpenAI's GPT Image 2.5 and get a transparent PNG
|
|
5
|
+
Author-email: Nick Powell <hey+remove-background-ai@nickmp.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/nmpowell/remove-background-ai
|
|
8
|
+
Project-URL: Issues, https://github.com/nmpowell/remove-background-ai/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/nmpowell/remove-background-ai/releases
|
|
10
|
+
Keywords: background-removal,openai,gpt-image,transparent-png,cli
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Topic :: Multimedia :: Graphics
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.14
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: click<9,>=8.5
|
|
22
|
+
Requires-Dist: openai<4,>=3.19.2
|
|
23
|
+
Requires-Dist: pillow<13,>=12.3
|
|
24
|
+
Requires-Dist: pydantic<3,>=2.13
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# remove-background-ai
|
|
28
|
+
|
|
29
|
+
Remove the background from a photo with OpenAI's GPT Image 2.5 and get a transparent PNG, from the command line or from Python.
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
export OPENAI_API_KEY=sk-...
|
|
33
|
+
uvx remove-background-ai photo.jpg
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
That writes `photo-no-bg.png` next to `photo.jpg`: your original pixels, at their original size, with the background made transparent.
|
|
37
|
+
|
|
38
|
+
> ⚠️ Much of this is AI-generated, and not formally reviewed by hand or eye. It's published chiefly for myself: for my own reference, use, and for experimentation with the whole open-source publishing process. I also *use* this code: I dogfood it. It works, for me. I also write tests, and run them to check that it works, and does what it says.
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
Run it without installing, with [uv](https://docs.astral.sh/uv/):
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
uvx remove-background-ai photo.jpg
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Or install it:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install remove-background-ai
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
It needs Python 3.14 or later and an OpenAI API key with access to the GPT Image 2.5 models. Set the key in the `OPENAI_API_KEY` environment variable; the command has no option for it. The OpenAI SDK's other variables, such as `OPENAI_BASE_URL`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID`, work too.
|
|
55
|
+
|
|
56
|
+
Every image is a paid API call, billed by tokens. See [OpenAI's pricing](https://developers.openai.com/api/docs/pricing). The free tier cannot use GPT Image 2.5. A paid tier can, and OpenAI may ask you to verify your organisation first.
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
Remove the background from one image:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
remove-background-ai photo.jpg
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Say what to keep when the photo has more than one candidate subject, and what else to remove:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
remove-background-ai shelf.jpg --keep "the red backpack, including both straps" \
|
|
70
|
+
--remove "the stand and its shadow on the floor"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Process several images into a directory, and get the results as JSON:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
remove-background-ai *.jpg --output-dir cutouts --json
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Choose where one image goes, or replace an existing output:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
remove-background-ai photo.jpg -o cutout.png --force
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Keep the model's own image instead of your original pixels (see [How it works](#how-it-works)):
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
remove-background-ai photo.jpg --mode generated
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Use the faster model at a lower quality:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
remove-background-ai photo.jpg --model gpt-image-2.5-flare-2026-09-08 --quality medium
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The command prints each output path on standard output, one per line, and its progress and errors on standard error. It never overwrites a file unless you pass `--force`, and it checks for an existing output before it spends money on an API call.
|
|
98
|
+
|
|
99
|
+
## How it works
|
|
100
|
+
|
|
101
|
+
The command sends your image to the [image edit endpoint](https://developers.openai.com/api/reference/python/resources/images/methods/edit) with a prompt that says what to keep and what to remove, and asks for a PNG with a transparent background. There is no dedicated background-removal endpoint: GPT Image 2.5 redraws the image with the background removed.
|
|
102
|
+
|
|
103
|
+
The model redraws the whole image, so it can soften a label, move an edge or change a texture. By default the command keeps only the model's transparency and puts it on your original pixels. You choose between two output modes:
|
|
104
|
+
|
|
105
|
+
| Mode | What you get | Size |
|
|
106
|
+
| --- | --- | --- |
|
|
107
|
+
| `original` (default) | Your pixels, with the model's alpha channel | Your image's size |
|
|
108
|
+
| `generated` | The PNG the model returned, byte for byte | The size sent to the model |
|
|
109
|
+
|
|
110
|
+
In `original` mode the transparency is still the model's work. An edge can land a few pixels off, and a thin band of the old background can survive along the subject's outline: in testing, a strip of brick wall stayed along the top of a backpack. `generated` mode gives cleaner edges, because the model redraws them. Look at the result on a dark and a light backdrop before you rely on it. `generated` mode keeps the provenance metadata (C2PA) that OpenAI embeds in the PNG. `original` mode writes a new file, so that metadata is lost.
|
|
111
|
+
|
|
112
|
+
### Image sizes
|
|
113
|
+
|
|
114
|
+
GPT Image 2.5 only produces images whose width and height are multiples of 16, with an aspect ratio no wider than 3:1 and an area of 655,360 to 8,294,400 pixels. Anything over 3,686,400 pixels (2560×1440) is experimental. The command sends your image at the nearest size the model can produce, never an experimental one, and scales the returned transparency back to your image's size.
|
|
115
|
+
|
|
116
|
+
| Your image | Sent to the model | Why |
|
|
117
|
+
| --- | --- | --- |
|
|
118
|
+
| 1024×1024 | 1024×1024 | Already a size the model produces |
|
|
119
|
+
| 1920×1080 | 1920×1088 | 1080 is not a multiple of 16 |
|
|
120
|
+
| 512×512 | 816×816 | Below the minimum area |
|
|
121
|
+
| 6000×4000 | 2336×1552 | Above the largest standard area |
|
|
122
|
+
| 3000×900 | refused | Wider than 3:1 |
|
|
123
|
+
|
|
124
|
+
Sending 1920×1080 as 1920×1088 changes the aspect ratio slightly. Scaling the transparency back to 1920×1080 undoes that change, so the cutout does not shift.
|
|
125
|
+
|
|
126
|
+
The command first turns the image upright using its EXIF orientation. It accepts any format Pillow can open (JPEG, PNG, WebP, TIFF and others) in any colour mode, and always writes an 8-bit RGBA PNG. If your image already has transparency, the output never makes it more opaque. An embedded RGB colour profile goes to the model with the image and stays in the `original` output. Other profiles would describe the wrong colours after conversion, so they are dropped. CMYK images are converted to RGB without colour management, so their colours can shift; convert them to RGB yourself first if colour matters. EXIF data is not copied. Animated images are refused.
|
|
127
|
+
|
|
128
|
+
### Masks
|
|
129
|
+
|
|
130
|
+
If the model keeps the wrong thing, give it a mask: an image the same size as the photo (after EXIF rotation), white where the subject is and black elsewhere.
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
remove-background-ai photo.jpg --mask subject.png
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Grey at half intensity or brighter counts as white. The mask guides the model, which can still stray over its edges.
|
|
137
|
+
|
|
138
|
+
## JSON output
|
|
139
|
+
|
|
140
|
+
`--json` replaces the list of paths with one JSON array. Each image gets an object, in the order given, whether it succeeded or failed:
|
|
141
|
+
|
|
142
|
+
```json
|
|
143
|
+
[
|
|
144
|
+
{
|
|
145
|
+
"input": "mug.jpg",
|
|
146
|
+
"output": "mug-no-bg.png",
|
|
147
|
+
"mode": "original",
|
|
148
|
+
"model": "gpt-image-2.5-sunburst-2026-09-08",
|
|
149
|
+
"quality": "medium",
|
|
150
|
+
"source_size": "1300x1132",
|
|
151
|
+
"model_size": "1312x1136",
|
|
152
|
+
"output_size": "1300x1132",
|
|
153
|
+
"request_id": "req_e4351b4660894c96ad6f08a86fc013aa",
|
|
154
|
+
"usage": {
|
|
155
|
+
"input_tokens": 1618,
|
|
156
|
+
"output_tokens": 440,
|
|
157
|
+
"total_tokens": 2058,
|
|
158
|
+
"input_tokens_details": {"image_tokens": 1476, "text_tokens": 142},
|
|
159
|
+
"output_tokens_details": {"image_tokens": 440, "text_tokens": 0}
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
"input": "broken.png",
|
|
164
|
+
"error": "broken.png cannot be read as an image: cannot identify image file 'broken.png'"
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
`usage` is whatever token counts the API reported, or `null`. Errors also go to standard error, so keep the two streams apart when you parse the JSON.
|
|
170
|
+
|
|
171
|
+
## Exit codes
|
|
172
|
+
|
|
173
|
+
| Code | Meaning |
|
|
174
|
+
| --- | --- |
|
|
175
|
+
| 0 | Every image succeeded |
|
|
176
|
+
| 1 | Interrupted (Ctrl-C), or a broken pipe |
|
|
177
|
+
| 2 | The command line was wrong: an unknown option, a missing file, `-o` or `--mask` with several images, `-o` with `--output-dir`, an output that is also an input or the mask, or two images that would write the same output |
|
|
178
|
+
| 3 | The tool refused an image or an output before or after the API call: it cannot be read, is animated, is wider than 3:1, its mask does not match, or the output exists or cannot be written |
|
|
179
|
+
| 4 | The OpenAI call failed or returned an unusable image, or `OPENAI_API_KEY` is not set |
|
|
180
|
+
|
|
181
|
+
With several images the command carries on after a failure. If any image failed, it exits with 4 when at least one failure was an API failure, and 3 otherwise.
|
|
182
|
+
|
|
183
|
+
## Timeouts and retries
|
|
184
|
+
|
|
185
|
+
The OpenAI client waits up to 300 seconds on each network operation and retries twice after a rate limit, a server error, a dropped connection or a timeout, pausing between attempts as the server asks. There is no overall deadline, so a slow image can take many minutes. A retried request can be billed more than once, because the server may have finished the first attempt.
|
|
186
|
+
|
|
187
|
+
## Python library
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from pathlib import Path
|
|
191
|
+
|
|
192
|
+
from remove_background_ai import RemovalOptions, remove_background
|
|
193
|
+
|
|
194
|
+
cutout = remove_background(
|
|
195
|
+
Path("photo.jpg"),
|
|
196
|
+
options=RemovalOptions(keep="the red backpack", quality="medium"),
|
|
197
|
+
)
|
|
198
|
+
Path("photo-no-bg.png").write_bytes(cutout.png)
|
|
199
|
+
print(cutout.model_size, cutout.request_id)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
`remove_background` validates its arguments with Pydantic and raises `pydantic.ValidationError` for the wrong types. It takes a `Path` (not a `str`) as its only positional argument; `options`, `mask` and `editor` are keyword-only. `RemovalOptions` takes `model` (default `gpt-image-2.5-sunburst-2026-09-08`), `quality` (`low`, `medium`, `high`, `xhigh`, `max` or `auto`; default `high`), `mode` (`original` or `generated`), `keep` and `remove`.
|
|
203
|
+
|
|
204
|
+
It returns a `Cutout` holding the PNG bytes and their metadata; `cutout.model_dump(mode="json")` gives the metadata shown under [JSON output](#json-output), without the image. It raises `InvalidInputError` when it refuses an image and `EditError` when the API call fails; both are `RemoveBackgroundError`s.
|
|
205
|
+
|
|
206
|
+
To change the timeout or retries, pass your own client:
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
from pathlib import Path
|
|
210
|
+
|
|
211
|
+
from openai import OpenAI
|
|
212
|
+
|
|
213
|
+
from remove_background_ai import OpenAIImageEditor, remove_background
|
|
214
|
+
|
|
215
|
+
editor = OpenAIImageEditor(OpenAI(timeout=600, max_retries=0))
|
|
216
|
+
cutout = remove_background(Path("photo.jpg"), editor=editor)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Any object with an `edit(EditRequest) -> EditResponse` method can stand in for `OpenAIImageEditor`, which is how the tests run without the API.
|
|
220
|
+
|
|
221
|
+
## CLI reference
|
|
222
|
+
|
|
223
|
+
<!-- [[[cog
|
|
224
|
+
import cog
|
|
225
|
+
from click.testing import CliRunner
|
|
226
|
+
from remove_background_ai.cli import cli
|
|
227
|
+
|
|
228
|
+
result = CliRunner().invoke(cli, ["--help"], terminal_width=88)
|
|
229
|
+
assert result.exit_code == 0, result.output
|
|
230
|
+
cog.out("```\n" + result.stdout.replace("Usage: cli", "Usage: remove-background-ai") + "```\n")
|
|
231
|
+
]]] -->
|
|
232
|
+
```
|
|
233
|
+
Usage: remove-background-ai [OPTIONS] IMAGES...
|
|
234
|
+
|
|
235
|
+
Send each image to GPT Image 2.5 and write a transparent PNG. By default, the result
|
|
236
|
+
keeps the original pixels and size and takes only transparency from the model. Set
|
|
237
|
+
OPENAI_API_KEY before running this command.
|
|
238
|
+
|
|
239
|
+
Examples:
|
|
240
|
+
|
|
241
|
+
remove-background-ai photo.jpg
|
|
242
|
+
remove-background-ai photo.jpg -o portrait.png
|
|
243
|
+
remove-background-ai photo.jpg other.png -d cutouts --json
|
|
244
|
+
remove-background-ai photo.jpg --keep "the red backpack, including both straps"
|
|
245
|
+
|
|
246
|
+
Options:
|
|
247
|
+
-o, --output FILE Write the PNG to FILE.
|
|
248
|
+
-d, --output-dir DIRECTORY Write PNGs to DIRECTORY.
|
|
249
|
+
--force Replace output files that already exist.
|
|
250
|
+
--keep TEXT What to keep, for example the red backpack and its
|
|
251
|
+
straps.
|
|
252
|
+
--remove TEXT What to remove besides the background.
|
|
253
|
+
--mask FILE Mask FILE: white keeps, black removes; same size as
|
|
254
|
+
the image.
|
|
255
|
+
--mode [original|generated] Keep original pixels and size, or use the generated
|
|
256
|
+
image. [default: original]
|
|
257
|
+
--model [gpt-image-2.5-sunburst-2026-09-08|gpt-image-2.5-sunburst|gpt-image-2.5-flare-2026-09-08|gpt-image-2.5-flare]
|
|
258
|
+
GPT Image model to use. [default: gpt-
|
|
259
|
+
image-2.5-sunburst-2026-09-08]
|
|
260
|
+
--quality [low|medium|high|xhigh|max|auto]
|
|
261
|
+
Image edit quality. [default: high]
|
|
262
|
+
--json Write a JSON array to stdout.
|
|
263
|
+
--version Show the version and exit.
|
|
264
|
+
--help Show this message and exit.
|
|
265
|
+
|
|
266
|
+
Exit codes: 0 success; 1 general failure; 2 usage error; 3 input or output refused; 4
|
|
267
|
+
image edit failed.
|
|
268
|
+
```
|
|
269
|
+
<!-- [[[end]]] -->
|
|
270
|
+
|
|
271
|
+
## Development
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
git clone https://github.com/nmpowell/remove-background-ai
|
|
275
|
+
cd remove-background-ai
|
|
276
|
+
uv sync --locked
|
|
277
|
+
uv run pytest
|
|
278
|
+
uv run mypy
|
|
279
|
+
uv run ruff check .
|
|
280
|
+
uv run ruff format --check .
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
The tests run without the API, except one live test that makes a real call and costs one image. It is skipped unless you ask for it:
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
RUN_LIVE_OPENAI_TESTS=1 OPENAI_API_KEY=sk-... uv run pytest -m live
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
After changing the command's options, regenerate the CLI reference above with `uv run cog -r README.md`. The test suite fails while the reference is out of date.
|
|
290
|
+
|
|
291
|
+
## Licence
|
|
292
|
+
|
|
293
|
+
Apache-2.0. See [LICENSE](LICENSE).
|