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
pip install openai Env vars
OPENAI_API_KEY Imports
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
- Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY
- Import the OpenAI client and initialize it with your API key from os.environ
- Call the images.generate method with model='dall-e-3' and provide a descriptive prompt
- Specify the number of images and size if needed (e.g., 1024x1024)
- Extract the image URL from the response's data array
- Use or display the returned image URL as needed
Full code
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
{"model": "dall-e-3", "prompt": "A futuristic cityscape at sunset", "n": 1, "size": "1024x1024"} Response
{"created": 1680000000, "data": [{"url": "https://openai-generated-images.s3.amazonaws.com/abc123def456.png"}]} Extract
response.data[0].urlVariants
Generate multiple images at once ›
When you want to generate multiple image variations for the same prompt in one API call.
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.
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.
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.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| Single image generation | ~2-5s | ~$0.02 | Standard image generation |
| Multiple images per call | ~3-7s | ~$0.02 x n | Batch image variations |
| Smaller image size (512x512) | ~1-3s | ~$0.01 | Faster, 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.