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