python/resources/images/methods/generate/index.md +0 −390 deleted
File Deleted View Diff
1## Create image
2
3`images.generate(ImageGenerateParams**kwargs) -> ImagesResponse`
4
5**post** `/images/generations`
6
7Creates an image given a prompt. [Learn more](https://platform.openai.com/docs/guides/images).
8
9### Parameters
10
11- `prompt: str`
12
13 A text description of the desired image(s). The maximum length is 32000 characters for the GPT image models, 1000 characters for `dall-e-2` and 4000 characters for `dall-e-3`.
14
15- `background: Optional[Literal["transparent", "opaque", "auto"]]`
16
17 Allows to set transparency for the background of the generated image(s).
18 This parameter is only supported for GPT image models that support
19 transparent backgrounds. Must be one of `transparent`, `opaque`, or
20 `auto` (default value). When `auto` is used, the model will
21 automatically determine the best background for the image.
22
23 `gpt-image-2` and `gpt-image-2-2026-04-21` do not support
24 transparent backgrounds. Requests with `background` set to
25 `transparent` will return an error for these models; use `opaque` or
26 `auto` instead.
27
28 If `transparent`, the output format needs to support transparency,
29 so it should be set to either `png` (default value) or `webp`.
30
31 - `"transparent"`
32
33 - `"opaque"`
34
35 - `"auto"`
36
37- `model: Optional[Union[str, ImageModel, null]]`
38
39 The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or a GPT image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`, `gpt-image-2`, or `gpt-image-2-2026-04-21`). Defaults to `dall-e-2` unless a parameter specific to the GPT image models is used.
40
41 - `str`
42
43 - `Literal["gpt-image-1", "gpt-image-1-mini", "gpt-image-2", 5 more]`
44
45 - `"gpt-image-1"`
46
47 - `"gpt-image-1-mini"`
48
49 - `"gpt-image-2"`
50
51 - `"gpt-image-2-2026-04-21"`
52
53 - `"gpt-image-1.5"`
54
55 - `"chatgpt-image-latest"`
56
57 - `"dall-e-2"`
58
59 - `"dall-e-3"`
60
61- `moderation: Optional[Literal["low", "auto"]]`
62
63 Control the content-moderation level for images generated by the GPT image models. Must be either `low` for less restrictive filtering or `auto` (default value).
64
65 - `"low"`
66
67 - `"auto"`
68
69- `n: Optional[int]`
70
71 The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
72
73- `output_compression: Optional[int]`
74
75 The compression level (0-100%) for the generated images. This parameter is only supported for the GPT image models with the `webp` or `jpeg` output formats, and defaults to 100.
76
77- `output_format: Optional[Literal["png", "jpeg", "webp"]]`
78
79 The format in which the generated images are returned. This parameter is only supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
80
81 - `"png"`
82
83 - `"jpeg"`
84
85 - `"webp"`
86
87- `partial_images: Optional[int]`
88
89 The number of partial images to generate. This parameter is used for
90 streaming responses that return partial images. Value must be between 0 and 3.
91 When set to 0, the response will be a single image sent in one streaming event.
92
93 Note that the final image may be sent before the full number of partial images
94 are generated if the full image is generated more quickly.
95
96- `quality: Optional[Literal["standard", "hd", "low", 3 more]]`
97
98 The quality of the image that will be generated.
99
100 - `auto` (default value) will automatically select the best quality for the given model.
101 - `high`, `medium` and `low` are supported for the GPT image models.
102 - `hd` and `standard` are supported for `dall-e-3`.
103 - `standard` is the only option for `dall-e-2`.
104
105 - `"standard"`
106
107 - `"hd"`
108
109 - `"low"`
110
111 - `"medium"`
112
113 - `"high"`
114
115 - `"auto"`
116
117- `response_format: Optional[Literal["url", "b64_json"]]`
118
119 The format in which generated images with `dall-e-2` and `dall-e-3` 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 isn't supported for the GPT image models, which always return base64-encoded images.
120
121 - `"url"`
122
123 - `"b64_json"`
124
125- `size: Optional[Union[str, Literal["auto", "1024x1024", "1536x1024", 5 more], null]]`
126
127 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`.
128
129 - `str`
130
131 - `Literal["auto", "1024x1024", "1536x1024", 5 more]`
132
133 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`.
134
135 - `"auto"`
136
137 - `"1024x1024"`
138
139 - `"1536x1024"`
140
141 - `"1024x1536"`
142
143 - `"256x256"`
144
145 - `"512x512"`
146
147 - `"1792x1024"`
148
149 - `"1024x1792"`
150
151- `stream: Optional[Literal[false]]`
152
153 Generate the image in streaming mode. Defaults to `false`. See the
154 [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more information.
155 This parameter is only supported for the GPT image models.
156
157 - `false`
158
159- `style: Optional[Literal["vivid", "natural"]]`
160
161 The style of the generated images. This parameter is only supported for `dall-e-3`. Must be one of `vivid` or `natural`. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images.
162
163 - `"vivid"`
164
165 - `"natural"`
166
167- `user: Optional[str]`
168
169 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).
170
171### Returns
172
173- `class ImagesResponse: …`
174
175 The response from the image generation endpoint.
176
177 - `created: int`
178
179 The Unix timestamp (in seconds) of when the image was created.
180
181 - `background: Optional[Literal["transparent", "opaque"]]`
182
183 The background parameter used for the image generation. Either `transparent` or `opaque`.
184
185 - `"transparent"`
186
187 - `"opaque"`
188
189 - `data: Optional[List[Image]]`
190
191 The list of generated images.
192
193 - `b64_json: Optional[str]`
194
195 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`.
196
197 - `revised_prompt: Optional[str]`
198
199 For `dall-e-3` only, the revised prompt that was used to generate the image.
200
201 - `url: Optional[str]`
202
203 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.
204
205 - `output_format: Optional[Literal["png", "webp", "jpeg"]]`
206
207 The output format of the image generation. Either `png`, `webp`, or `jpeg`.
208
209 - `"png"`
210
211 - `"webp"`
212
213 - `"jpeg"`
214
215 - `quality: Optional[Literal["low", "medium", "high"]]`
216
217 The quality of the image generated. Either `low`, `medium`, or `high`.
218
219 - `"low"`
220
221 - `"medium"`
222
223 - `"high"`
224
225 - `size: Optional[Literal["1024x1024", "1024x1536", "1536x1024"]]`
226
227 The size of the image generated. Either `1024x1024`, `1024x1536`, or `1536x1024`.
228
229 - `"1024x1024"`
230
231 - `"1024x1536"`
232
233 - `"1536x1024"`
234
235 - `usage: Optional[Usage]`
236
237 For `gpt-image-1` only, the token usage information for the image generation.
238
239 - `input_tokens: int`
240
241 The number of tokens (images and text) in the input prompt.
242
243 - `input_tokens_details: UsageInputTokensDetails`
244
245 The input tokens detailed information for the image generation.
246
247 - `image_tokens: int`
248
249 The number of image tokens in the input prompt.
250
251 - `text_tokens: int`
252
253 The number of text tokens in the input prompt.
254
255 - `output_tokens: int`
256
257 The number of output tokens generated by the model.
258
259 - `total_tokens: int`
260
261 The total number of tokens (images and text) used for the image generation.
262
263 - `output_tokens_details: Optional[UsageOutputTokensDetails]`
264
265 The output token details for the image generation.
266
267 - `image_tokens: int`
268
269 The number of image output tokens generated by the model.
270
271 - `text_tokens: int`
272
273 The number of text output tokens generated by the model.
274
275### Example
276
277```python
278import os
279from openai import OpenAI
280
281client = OpenAI(
282 api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
283)
284for image in client.images.generate(
285 prompt="A cute baby sea otter",
286):
287 print(image)
288```
289
290#### Response
291
292```json
293{
294 "created": 0,
295 "background": "transparent",
296 "data": [
297 {
298 "b64_json": "b64_json",
299 "revised_prompt": "revised_prompt",
300 "url": "https://example.com"
301 }
302 ],
303 "output_format": "png",
304 "quality": "low",
305 "size": "1024x1024",
306 "usage": {
307 "input_tokens": 0,
308 "input_tokens_details": {
309 "image_tokens": 0,
310 "text_tokens": 0
311 },
312 "output_tokens": 0,
313 "total_tokens": 0,
314 "output_tokens_details": {
315 "image_tokens": 0,
316 "text_tokens": 0
317 }
318 }
319}
320```
321
322### Generate image
323
324```python
325import base64
326from openai import OpenAI
327client = OpenAI()
328
329img = client.images.generate(
330 model="gpt-image-1.5",
331 prompt="A cute baby sea otter",
332 n=1,
333 size="1024x1024"
334)
335
336image_bytes = base64.b64decode(img.data[0].b64_json)
337with open("output.png", "wb") as f:
338 f.write(image_bytes)
339```
340
341#### Response
342
343```json
344{
345 "created": 1713833628,
346 "data": [
347 {
348 "b64_json": "..."
349 }
350 ],
351 "usage": {
352 "total_tokens": 100,
353 "input_tokens": 50,
354 "output_tokens": 50,
355 "input_tokens_details": {
356 "text_tokens": 10,
357 "image_tokens": 40
358 }
359 }
360}
361```
362
363### Streaming
364
365```python
366from openai import OpenAI
367
368client = OpenAI()
369
370stream = client.images.generate(
371 model="gpt-image-1.5",
372 prompt="A cute baby sea otter",
373 n=1,
374 size="1024x1024",
375 stream=True
376)
377
378for event in stream:
379 print(event)
380```
381
382#### Response
383
384```json
385event: image_generation.partial_image
386data: {"type":"image_generation.partial_image","b64_json":"...","partial_image_index":0}
387
388event: image_generation.completed
389data: {"type":"image_generation.completed","b64_json":"...","usage":{"total_tokens":100,"input_tokens":50,"output_tokens":50,"input_tokens_details":{"text_tokens":10,"image_tokens":40}}}
390```