Images
Generate Images
POST
/
images
/
generations
Generate Images
curl --request POST \
--url https://api.example.com/images/generationsimport requests
url = "https://api.example.com/images/generations"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://api.example.com/images/generations', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/images/generations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/images/generations"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/images/generations")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/images/generations")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_body{
"created": 123,
"data": [
{
"url": "<string>",
"b64_json": "<string>",
"revised_prompt": "<string>"
}
],
"usage": {}
}Creates an image given a prompt.
Method Signature
client.images.generate(
prompt="A cute cat",
model="dall-e-3",
size="1024x1024",
quality="hd",
n=1,
response_format="url",
style="vivid"
)
Parameters
string
required
A text description of the desired image(s). The maximum length is:
- 32000 characters for GPT image models
- 4000 characters for
dall-e-3 - 1000 characters for
dall-e-2
string
The model to use for image generation. Options:
dall-e-2dall-e-3gpt-image-1gpt-image-1-minigpt-image-1.5chatgpt-image-latest
dall-e-2 unless a parameter specific to the GPT image models is used.integer
The number of images to generate. Must be between 1 and 10. For
dall-e-3, only n=1 is supported.string
The quality of the image that will be generated:
auto(default) - automatically select the best quality for the given modelhigh,medium,low- supported for GPT image modelshd,standard- supported fordall-e-3standard- only option fordall-e-2
string
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 and dall-e-3 - GPT image models always return base64-encoded images.string
The size of the generated images:
- GPT image models:
1024x1024,1536x1024(landscape),1024x1536(portrait), orauto(default) dall-e-2:256x256,512x512, or1024x1024dall-e-3:1024x1024,1792x1024, or1024x1792
string
The style of the generated images. Only supported for
dall-e-3:vivid- generates hyper-real and dramatic imagesnatural- produces more natural, less hyper-real looking images
string
Allows to set transparency for the background of the generated image(s). Only supported for GPT image models:
transparentopaqueauto(default) - model automatically determines the best background
transparent, the output format needs to support transparency (png or webp).string
The format in which the generated images are returned. Only supported for GPT image models:
png(default)jpegwebp
integer
The compression level (0-100%) for the generated images. Only supported for GPT image models with
webp or jpeg output formats. Defaults to 100.string
Control the content-moderation level for images generated by the GPT image models:
auto(default)low- less restrictive filtering
boolean
Generate the image in streaming mode. Defaults to
false. Only supported for GPT image models. See the Image generation guide for more information.integer
The number of partial images to generate. Used for streaming responses that return partial images. Value must be between 0 and 3. When set to 0, the response will be a single image sent in one streaming event.Note that the final image may be sent before the full number of partial images are generated if the full image is generated more quickly.
string
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
Returns
Returns anImagesResponse object containing the generated images.
integer
The Unix timestamp (in seconds) of when the image was created.
array
The list of generated images.
Show Image object
Show Image object
string
The URL of the generated image (for
dall-e-2 and dall-e-3 with response_format=url). URLs are only valid for 60 minutes.string
The base64-encoded JSON of the generated image. Returned by default for GPT image models, or when
response_format=b64_json for DALL-E models.string
For
dall-e-3 only, the revised prompt that was used to generate the image.object
For
gpt-image-1 only, the token usage information for the image generation.Examples
Generate with DALL-E 3 (URL response)
from openai import OpenAI
client = OpenAI()
response = client.images.generate(
model="dall-e-3",
prompt="A futuristic cityscape at sunset",
size="1024x1024",
quality="hd",
n=1
)
image_url = response.data[0].url
print(f"Image URL: {image_url}")
Generate with DALL-E 2 (base64 response)
import base64
from openai import OpenAI
client = OpenAI()
response = client.images.generate(
model="dall-e-2",
prompt="A white siamese cat",
size="512x512",
response_format="b64_json",
n=2
)
for i, image in enumerate(response.data):
image_data = base64.b64decode(image.b64_json)
with open(f"image_{i}.png", "wb") as f:
f.write(image_data)
Generate with GPT Image model
from openai import OpenAI
client = OpenAI()
response = client.images.generate(
model="gpt-image-1.5",
prompt="A minimalist logo design for a tech startup",
size="1024x1024",
quality="high",
background="transparent",
output_format="png"
)
# GPT image models always return base64
import base64
image_data = base64.b64decode(response.data[0].b64_json)
with open("logo.png", "wb") as f:
f.write(image_data)
Streaming generation
from openai import OpenAI
client = OpenAI()
stream = client.images.generate(
model="gpt-image-1",
prompt="A scenic mountain landscape",
stream=True,
partial_images=2
)
for event in stream:
if event.type == "partial_image":
print(f"Received partial image at {event.partial_image.progress}% progress")
elif event.type == "completed":
print("Image generation completed!")
image_data = event.data[0].b64_json
Generate Images
curl --request POST \
--url https://api.example.com/images/generationsimport requests
url = "https://api.example.com/images/generations"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://api.example.com/images/generations', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/images/generations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/images/generations"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/images/generations")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/images/generations")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_body{
"created": 123,
"data": [
{
"url": "<string>",
"b64_json": "<string>",
"revised_prompt": "<string>"
}
],
"usage": {}
}