Code beginner · 3 min read

How to generate images with DALL-E 3 in python

Direct answer
Use the OpenAI Python SDK with client.images.generate specifying model="dall-e-3" and a prompt to generate images with DALL-E 3.

Setup

Install
bash
pip install openai
Env vars
OPENAI_API_KEY
Imports
python
import os
from openai import OpenAI

Examples

inA futuristic cityscape at sunset
outURL to generated image of a futuristic cityscape at sunset
inA cute golden retriever puppy playing in the snow
outURL to generated image of a golden retriever puppy in snow
inAn astronaut riding a horse on Mars
outURL to generated image of astronaut on horse on Mars

Integration steps

  1. Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY
  2. Import the OpenAI client and initialize it with your API key from os.environ
  3. Call the images.generate method with model='dall-e-3' and provide a descriptive prompt
  4. Specify the number of images and size if needed (e.g., 1024x1024)
  5. Extract the image URL from the response's data array
  6. Use or display the returned image URL as needed

Full code

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.images.generate(
    model="dall-e-3",
    prompt="A futuristic cityscape at sunset",
    n=1,
    size="1024x1024"
)

image_url = response.data[0].url
print("Generated image URL:", image_url)
output
Generated image URL: https://openai-generated-images.s3.amazonaws.com/abc123def456.png

API trace

Request
json
{"model": "dall-e-3", "prompt": "A futuristic cityscape at sunset", "n": 1, "size": "1024x1024"}
Response
json
{"created": 1680000000, "data": [{"url": "https://openai-generated-images.s3.amazonaws.com/abc123def456.png"}]}
Extractresponse.data[0].url

Variants

Generate multiple images at once

When you want to generate multiple image variations for the same prompt in one API call.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.images.generate(
    model="dall-e-3",
    prompt="A futuristic cityscape at sunset",
    n=3,
    size="1024x1024"
)

for i, img in enumerate(response.data):
    print(f"Image {i+1} URL:", img.url)
Generate smaller images for faster response

When you want faster generation and lower cost by requesting smaller image sizes.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.images.generate(
    model="dall-e-3",
    prompt="A futuristic cityscape at sunset",
    n=1,
    size="512x512"
)

print("Generated image URL:", response.data[0].url)
Async image generation

When integrating image generation into an async Python application for concurrency.

python
import os
import asyncio
from openai import OpenAI

async def generate_image():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    response = await client.images.generate(
        model="dall-e-3",
        prompt="A futuristic cityscape at sunset",
        n=1,
        size="1024x1024"
    )
    print("Generated image URL:", response.data[0].url)

asyncio.run(generate_image())

Performance

Latency~2-5 seconds per image generation request
Cost~$0.02 per 1024x1024 image generated
Rate limitsDefault tier: 60 requests per minute, 1000 requests per day
  • Keep prompts concise but descriptive to avoid unnecessary token usage.
  • Generate fewer images per request to reduce cost.
  • Use smaller image sizes (512x512) for faster and cheaper generation.
ApproachLatencyCost/callBest for
Single image generation~2-5s~$0.02Standard image generation
Multiple images per call~3-7s~$0.02 x nBatch image variations
Smaller image size (512x512)~1-3s~$0.01Faster, cheaper previews

Quick tip

Use descriptive, detailed prompts to get higher quality and more relevant images from DALL-E 3.

Common mistake

Forgetting to specify the correct model name "dall-e-3" or missing the API key in environment variables causes authentication or model errors.

Verified 2026-04 · dall-e-3
Verify ↗