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
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
The model to use for image generation. Options:
dall-e-2
dall-e-3
gpt-image-1
gpt-image-1-mini
gpt-image-1.5
chatgpt-image-latest
Defaults to dall-e-2 unless a parameter specific to the GPT image models is used.
The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1 is supported.
The quality of the image that will be generated:
auto (default) - automatically select the best quality for the given model
high, medium, low - supported for GPT image models
hd, standard - supported for dall-e-3
standard - only option for dall-e-2
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.
The size of the generated images:
GPT image models: 1024x1024, 1536x1024 (landscape), 1024x1536 (portrait), or auto (default)
dall-e-2: 256x256, 512x512, or 1024x1024
dall-e-3: 1024x1024, 1792x1024, or 1024x1792
The style of the generated images. Only supported for dall-e-3:
vivid - generates hyper-real and dramatic images
natural - produces more natural, less hyper-real looking images
Allows to set transparency for the background of the generated image(s). Only supported for GPT image models:
transparent
opaque
auto (default) - model automatically determines the best background
If transparent, the output format needs to support transparency (png or webp).
The format in which the generated images are returned. Only supported for GPT image models:
The compression level (0-100%) for the generated images. Only supported for GPT image models with webp or jpeg output formats. Defaults to 100.
Control the content-moderation level for images generated by the GPT image models:
auto (default)
low - less restrictive filtering
Generate the image in streaming mode. Defaults to false. Only supported for GPT image models. See the Image generation guide for more information.
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.
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more .
Returns
Returns an ImagesResponse object containing the generated images.
The Unix timestamp (in seconds) of when the image was created.
The list of generated images. 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.
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.
For dall-e-3 only, the revised prompt that was used to generate the image.
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