java/resources/images/methods/generate/index.md +0 −313 deleted
File Deleted View Diff
1## Create image
2
3`ImagesResponse images().generate(ImageGenerateParamsparams, RequestOptionsrequestOptions = RequestOptions.none())`
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- `ImageGenerateParams params`
12
13 - `String prompt`
14
15 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`.
16
17 - `Optional<Background> background`
18
19 Allows to set transparency for the background of the generated image(s).
20 This parameter is only supported for GPT image models that support
21 transparent backgrounds. Must be one of `transparent`, `opaque`, or
22 `auto` (default value). When `auto` is used, the model will
23 automatically determine the best background for the image.
24
25 `gpt-image-2` and `gpt-image-2-2026-04-21` do not support
26 transparent backgrounds. Requests with `background` set to
27 `transparent` will return an error for these models; use `opaque` or
28 `auto` instead.
29
30 If `transparent`, the output format needs to support transparency,
31 so it should be set to either `png` (default value) or `webp`.
32
33 - `TRANSPARENT("transparent")`
34
35 - `OPAQUE("opaque")`
36
37 - `AUTO("auto")`
38
39 - `Optional<ImageModel> model`
40
41 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.
42
43 - `GPT_IMAGE_1("gpt-image-1")`
44
45 - `GPT_IMAGE_1_MINI("gpt-image-1-mini")`
46
47 - `GPT_IMAGE_2("gpt-image-2")`
48
49 - `GPT_IMAGE_2_2026_04_21("gpt-image-2-2026-04-21")`
50
51 - `GPT_IMAGE_1_5("gpt-image-1.5")`
52
53 - `CHATGPT_IMAGE_LATEST("chatgpt-image-latest")`
54
55 - `DALL_E_2("dall-e-2")`
56
57 - `DALL_E_3("dall-e-3")`
58
59 - `Optional<Moderation> moderation`
60
61 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).
62
63 - `LOW("low")`
64
65 - `AUTO("auto")`
66
67 - `Optional<Long> n`
68
69 The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
70
71 - `Optional<Long> outputCompression`
72
73 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.
74
75 - `Optional<OutputFormat> outputFormat`
76
77 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`.
78
79 - `PNG("png")`
80
81 - `JPEG("jpeg")`
82
83 - `WEBP("webp")`
84
85 - `Optional<Long> partialImages`
86
87 The number of partial images to generate. This parameter is used for
88 streaming responses that return partial images. Value must be between 0 and 3.
89 When set to 0, the response will be a single image sent in one streaming event.
90
91 Note that the final image may be sent before the full number of partial images
92 are generated if the full image is generated more quickly.
93
94 - `Optional<Quality> quality`
95
96 The quality of the image that will be generated.
97
98 - `auto` (default value) will automatically select the best quality for the given model.
99 - `high`, `medium` and `low` are supported for the GPT image models.
100 - `hd` and `standard` are supported for `dall-e-3`.
101 - `standard` is the only option for `dall-e-2`.
102
103 - `STANDARD("standard")`
104
105 - `HD("hd")`
106
107 - `LOW("low")`
108
109 - `MEDIUM("medium")`
110
111 - `HIGH("high")`
112
113 - `AUTO("auto")`
114
115 - `Optional<ResponseFormat> responseFormat`
116
117 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.
118
119 - `URL("url")`
120
121 - `B64_JSON("b64_json")`
122
123 - `Optional<Size> size`
124
125 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`.
126
127 - `AUTO("auto")`
128
129 - `_1024X1024("1024x1024")`
130
131 - `_1536X1024("1536x1024")`
132
133 - `_1024X1536("1024x1536")`
134
135 - `_256X256("256x256")`
136
137 - `_512X512("512x512")`
138
139 - `_1792X1024("1792x1024")`
140
141 - `_1024X1792("1024x1792")`
142
143 - `Optional<Style> style`
144
145 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.
146
147 - `VIVID("vivid")`
148
149 - `NATURAL("natural")`
150
151 - `Optional<String> user`
152
153 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).
154
155### Returns
156
157- `class ImagesResponse:`
158
159 The response from the image generation endpoint.
160
161 - `long created`
162
163 The Unix timestamp (in seconds) of when the image was created.
164
165 - `Optional<Background> background`
166
167 The background parameter used for the image generation. Either `transparent` or `opaque`.
168
169 - `TRANSPARENT("transparent")`
170
171 - `OPAQUE("opaque")`
172
173 - `Optional<List<Image>> data`
174
175 The list of generated images.
176
177 - `Optional<String> b64Json`
178
179 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`.
180
181 - `Optional<String> revisedPrompt`
182
183 For `dall-e-3` only, the revised prompt that was used to generate the image.
184
185 - `Optional<String> url`
186
187 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.
188
189 - `Optional<OutputFormat> outputFormat`
190
191 The output format of the image generation. Either `png`, `webp`, or `jpeg`.
192
193 - `PNG("png")`
194
195 - `WEBP("webp")`
196
197 - `JPEG("jpeg")`
198
199 - `Optional<Quality> quality`
200
201 The quality of the image generated. Either `low`, `medium`, or `high`.
202
203 - `LOW("low")`
204
205 - `MEDIUM("medium")`
206
207 - `HIGH("high")`
208
209 - `Optional<Size> size`
210
211 The size of the image generated. Either `1024x1024`, `1024x1536`, or `1536x1024`.
212
213 - `_1024X1024("1024x1024")`
214
215 - `_1024X1536("1024x1536")`
216
217 - `_1536X1024("1536x1024")`
218
219 - `Optional<Usage> usage`
220
221 For `gpt-image-1` only, the token usage information for the image generation.
222
223 - `long inputTokens`
224
225 The number of tokens (images and text) in the input prompt.
226
227 - `InputTokensDetails inputTokensDetails`
228
229 The input tokens detailed information for the image generation.
230
231 - `long imageTokens`
232
233 The number of image tokens in the input prompt.
234
235 - `long textTokens`
236
237 The number of text tokens in the input prompt.
238
239 - `long outputTokens`
240
241 The number of output tokens generated by the model.
242
243 - `long totalTokens`
244
245 The total number of tokens (images and text) used for the image generation.
246
247 - `Optional<OutputTokensDetails> outputTokensDetails`
248
249 The output token details for the image generation.
250
251 - `long imageTokens`
252
253 The number of image output tokens generated by the model.
254
255 - `long textTokens`
256
257 The number of text output tokens generated by the model.
258
259### Example
260
261```java
262package com.openai.example;
263
264import com.openai.client.OpenAIClient;
265import com.openai.client.okhttp.OpenAIOkHttpClient;
266import com.openai.models.images.ImageGenerateParams;
267import com.openai.models.images.ImagesResponse;
268
269public final class Main {
270 private Main() {}
271
272 public static void main(String[] args) {
273 OpenAIClient client = OpenAIOkHttpClient.fromEnv();
274
275 ImageGenerateParams params = ImageGenerateParams.builder()
276 .prompt("A cute baby sea otter")
277 .build();
278 ImagesResponse imagesResponse = client.images().generate(params);
279 }
280}
281```
282
283#### Response
284
285```json
286{
287 "created": 0,
288 "background": "transparent",
289 "data": [
290 {
291 "b64_json": "b64_json",
292 "revised_prompt": "revised_prompt",
293 "url": "https://example.com"
294 }
295 ],
296 "output_format": "png",
297 "quality": "low",
298 "size": "1024x1024",
299 "usage": {
300 "input_tokens": 0,
301 "input_tokens_details": {
302 "image_tokens": 0,
303 "text_tokens": 0
304 },
305 "output_tokens": 0,
306 "total_tokens": 0,
307 "output_tokens_details": {
308 "image_tokens": 0,
309 "text_tokens": 0
310 }
311 }
312}
313```