python/resources/images/methods/edit/index.md +0 −396 deleted
File Deleted View Diff
1## Create image edit
2
3`images.edit(ImageEditParams**kwargs) -> ImagesResponse`
4
5**post** `/images/edits`
6
7Creates an edited or extended image given one or more source images and a prompt. This endpoint supports GPT Image models (`gpt-image-1.5`, `gpt-image-1`, `gpt-image-1-mini`, and `chatgpt-image-latest`) and `dall-e-2`.
8
9### Parameters
10
11- `image: Union[FileTypes, Sequence[FileTypes]]`
12
13 The image(s) to edit. Must be a supported image file or an array of images.
14
15 For the GPT image models (`gpt-image-1`, `gpt-image-1-mini`,
16 `gpt-image-1.5`, `gpt-image-2`, `gpt-image-2-2026-04-21`, and
17 `chatgpt-image-latest`), each image should be a `png`, `webp`, or
18 `jpg` file less than 50MB. You can provide up to 16 images.
19
20 For `dall-e-2`, you can only provide one image, and it should be a
21 square `png` file less than 4MB.
22
23 - `FileTypes`
24
25 - `Sequence[FileTypes]`
26
27- `prompt: str`
28
29 A text description of the desired image(s). The maximum length is 1000 characters for `dall-e-2`, and 32000 characters for the GPT image models.
30
31- `background: Optional[Literal["transparent", "opaque", "auto"]]`
32
33 Allows to set transparency for the background of the generated image(s).
34 This parameter is only supported for GPT image models that support
35 transparent backgrounds. Must be one of `transparent`, `opaque`, or
36 `auto` (default value). When `auto` is used, the model will
37 automatically determine the best background for the image.
38
39 `gpt-image-2` and `gpt-image-2-2026-04-21` do not support
40 transparent backgrounds. Requests with `background` set to
41 `transparent` will return an error for these models; use `opaque` or
42 `auto` instead.
43
44 If `transparent`, the output format needs to support transparency,
45 so it should be set to either `png` (default value) or `webp`.
46
47 - `"transparent"`
48
49 - `"opaque"`
50
51 - `"auto"`
52
53- `input_fidelity: Optional[Literal["high", "low"]]`
54
55 Control how much effort the model will exert to match the style and features, especially facial features, of input images. This parameter is only supported for `gpt-image-1` and `gpt-image-1.5` and later models, unsupported for `gpt-image-1-mini`. Supports `high` and `low`. Defaults to `low`.
56
57 - `"high"`
58
59 - `"low"`
60
61- `mask: Optional[FileTypes]`
62
63 An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where `image` should be edited. If there are multiple images provided, the mask will be applied on the first image. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`.
64
65- `model: Optional[Union[str, ImageModel, null]]`
66
67 The model to use for image generation. One of `dall-e-2` or a GPT image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`, `gpt-image-2`, `gpt-image-2-2026-04-21`, or `chatgpt-image-latest`). Defaults to `gpt-image-1.5`.
68
69 - `str`
70
71 - `Literal["gpt-image-1", "gpt-image-1-mini", "gpt-image-2", 5 more]`
72
73 - `"gpt-image-1"`
74
75 - `"gpt-image-1-mini"`
76
77 - `"gpt-image-2"`
78
79 - `"gpt-image-2-2026-04-21"`
80
81 - `"gpt-image-1.5"`
82
83 - `"chatgpt-image-latest"`
84
85 - `"dall-e-2"`
86
87 - `"dall-e-3"`
88
89- `n: Optional[int]`
90
91 The number of images to generate. Must be between 1 and 10.
92
93- `output_compression: Optional[int]`
94
95 The compression level (0-100%) for the generated images. This parameter
96 is only supported for the GPT image models with the `webp` or `jpeg` output
97 formats, and defaults to 100.
98
99- `output_format: Optional[Literal["png", "jpeg", "webp"]]`
100
101 The format in which the generated images are returned. This parameter is
102 only supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
103 The default value is `png`.
104
105 - `"png"`
106
107 - `"jpeg"`
108
109 - `"webp"`
110
111- `partial_images: Optional[int]`
112
113 The number of partial images to generate. This parameter is used for
114 streaming responses that return partial images. Value must be between 0 and 3.
115 When set to 0, the response will be a single image sent in one streaming event.
116
117 Note that the final image may be sent before the full number of partial images
118 are generated if the full image is generated more quickly.
119
120- `quality: Optional[Literal["standard", "low", "medium", 2 more]]`
121
122 The quality of the image that will be generated for GPT image models. Defaults to `auto`.
123
124 - `"standard"`
125
126 - `"low"`
127
128 - `"medium"`
129
130 - `"high"`
131
132 - `"auto"`
133
134- `response_format: Optional[Literal["url", "b64_json"]]`
135
136 The format in which the generated images are returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated. This parameter is only supported for `dall-e-2` (default is `url` for `dall-e-2`), as GPT image models always return base64-encoded images.
137
138 - `"url"`
139
140 - `"b64_json"`
141
142- `size: Optional[Union[str, Literal["256x256", "512x512", "1024x1024", 3 more], null]]`
143
144 The size of the generated images. For `gpt-image-2` and `gpt-image-2-2026-04-21`, arbitrary resolutions are supported as `WIDTHxHEIGHT` strings, for example `1536x864`. Width and height must both be divisible by 16 and the requested aspect ratio must be between 1:3 and 3:1. Resolutions above `2560x1440` are experimental, and the maximum supported resolution is `3840x2160`. The requested size must also satisfy the model's current pixel and edge limits. The standard sizes `1024x1024`, `1536x1024`, and `1024x1536` are supported by the GPT image models; `auto` is supported for models that allow automatic sizing. For `dall-e-2`, use one of `256x256`, `512x512`, or `1024x1024`. For `dall-e-3`, use one of `1024x1024`, `1792x1024`, or `1024x1792`.
145
146 - `str`
147
148 - `Literal["256x256", "512x512", "1024x1024", 3 more]`
149
150 The size of the generated images. For `gpt-image-2` and `gpt-image-2-2026-04-21`, arbitrary resolutions are supported as `WIDTHxHEIGHT` strings, for example `1536x864`. Width and height must both be divisible by 16 and the requested aspect ratio must be between 1:3 and 3:1. Resolutions above `2560x1440` are experimental, and the maximum supported resolution is `3840x2160`. The requested size must also satisfy the model's current pixel and edge limits. The standard sizes `1024x1024`, `1536x1024`, and `1024x1536` are supported by the GPT image models; `auto` is supported for models that allow automatic sizing. For `dall-e-2`, use one of `256x256`, `512x512`, or `1024x1024`. For `dall-e-3`, use one of `1024x1024`, `1792x1024`, or `1024x1792`.
151
152 - `"256x256"`
153
154 - `"512x512"`
155
156 - `"1024x1024"`
157
158 - `"1536x1024"`
159
160 - `"1024x1536"`
161
162 - `"auto"`
163
164- `stream: Optional[Literal[false]]`
165
166 Edit the image in streaming mode. Defaults to `false`. See the
167 [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more information.
168
169 - `false`
170
171- `user: Optional[str]`
172
173 A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
174
175### Returns
176
177- `class ImagesResponse: …`
178
179 The response from the image generation endpoint.
180
181 - `created: int`
182
183 The Unix timestamp (in seconds) of when the image was created.
184
185 - `background: Optional[Literal["transparent", "opaque"]]`
186
187 The background parameter used for the image generation. Either `transparent` or `opaque`.
188
189 - `"transparent"`
190
191 - `"opaque"`
192
193 - `data: Optional[List[Image]]`
194
195 The list of generated images.
196
197 - `b64_json: Optional[str]`
198
199 The base64-encoded JSON of the generated image. Returned by default for the GPT image models, and only present if `response_format` is set to `b64_json` for `dall-e-2` and `dall-e-3`.
200
201 - `revised_prompt: Optional[str]`
202
203 For `dall-e-3` only, the revised prompt that was used to generate the image.
204
205 - `url: Optional[str]`
206
207 When using `dall-e-2` or `dall-e-3`, the URL of the generated image if `response_format` is set to `url` (default value). Unsupported for the GPT image models.
208
209 - `output_format: Optional[Literal["png", "webp", "jpeg"]]`
210
211 The output format of the image generation. Either `png`, `webp`, or `jpeg`.
212
213 - `"png"`
214
215 - `"webp"`
216
217 - `"jpeg"`
218
219 - `quality: Optional[Literal["low", "medium", "high"]]`
220
221 The quality of the image generated. Either `low`, `medium`, or `high`.
222
223 - `"low"`
224
225 - `"medium"`
226
227 - `"high"`
228
229 - `size: Optional[Literal["1024x1024", "1024x1536", "1536x1024"]]`
230
231 The size of the image generated. Either `1024x1024`, `1024x1536`, or `1536x1024`.
232
233 - `"1024x1024"`
234
235 - `"1024x1536"`
236
237 - `"1536x1024"`
238
239 - `usage: Optional[Usage]`
240
241 For `gpt-image-1` only, the token usage information for the image generation.
242
243 - `input_tokens: int`
244
245 The number of tokens (images and text) in the input prompt.
246
247 - `input_tokens_details: UsageInputTokensDetails`
248
249 The input tokens detailed information for the image generation.
250
251 - `image_tokens: int`
252
253 The number of image tokens in the input prompt.
254
255 - `text_tokens: int`
256
257 The number of text tokens in the input prompt.
258
259 - `output_tokens: int`
260
261 The number of output tokens generated by the model.
262
263 - `total_tokens: int`
264
265 The total number of tokens (images and text) used for the image generation.
266
267 - `output_tokens_details: Optional[UsageOutputTokensDetails]`
268
269 The output token details for the image generation.
270
271 - `image_tokens: int`
272
273 The number of image output tokens generated by the model.
274
275 - `text_tokens: int`
276
277 The number of text output tokens generated by the model.
278
279### Example
280
281```python
282import os
283from openai import OpenAI
284
285client = OpenAI(
286 api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
287)
288for image in client.images.edit(
289 image=b"Example data",
290 prompt="A cute baby sea otter wearing a beret",
291):
292 print(image)
293```
294
295#### Response
296
297```json
298{
299 "created": 0,
300 "background": "transparent",
301 "data": [
302 {
303 "b64_json": "b64_json",
304 "revised_prompt": "revised_prompt",
305 "url": "https://example.com"
306 }
307 ],
308 "output_format": "png",
309 "quality": "low",
310 "size": "1024x1024",
311 "usage": {
312 "input_tokens": 0,
313 "input_tokens_details": {
314 "image_tokens": 0,
315 "text_tokens": 0
316 },
317 "output_tokens": 0,
318 "total_tokens": 0,
319 "output_tokens_details": {
320 "image_tokens": 0,
321 "text_tokens": 0
322 }
323 }
324}
325```
326
327### Edit image
328
329```python
330import base64
331from openai import OpenAI
332client = OpenAI()
333
334prompt = """
335Generate a photorealistic image of a gift basket on a white background
336labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
337containing all the items in the reference pictures.
338"""
339
340result = client.images.edit(
341 model="gpt-image-1.5",
342 image=[
343 open("body-lotion.png", "rb"),
344 open("bath-bomb.png", "rb"),
345 open("incense-kit.png", "rb"),
346 open("soap.png", "rb"),
347 ],
348 prompt=prompt
349)
350
351image_base64 = result.data[0].b64_json
352image_bytes = base64.b64decode(image_base64)
353
354# Save the image to a file
355with open("gift-basket.png", "wb") as f:
356 f.write(image_bytes)
357```
358
359### Streaming
360
361```python
362from openai import OpenAI
363
364client = OpenAI()
365
366prompt = """
367Generate a photorealistic image of a gift basket on a white background
368labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
369containing all the items in the reference pictures.
370"""
371
372stream = client.images.edit(
373 model="gpt-image-1.5",
374 image=[
375 open("body-lotion.png", "rb"),
376 open("bath-bomb.png", "rb"),
377 open("incense-kit.png", "rb"),
378 open("soap.png", "rb"),
379 ],
380 prompt=prompt,
381 stream=True
382)
383
384for event in stream:
385 print(event)
386```
387
388#### Response
389
390```json
391event: image_edit.partial_image
392data: {"type":"image_edit.partial_image","b64_json":"...","partial_image_index":0}
393
394event: image_edit.completed
395data: {"type":"image_edit.completed","b64_json":"...","usage":{"total_tokens":100,"input_tokens":50,"output_tokens":50,"input_tokens_details":{"text_tokens":10,"image_tokens":40}}}
396```