Image generation
Generate images from text prompts using the OpenAI-compatible Images API.
What you’ll learn:
- How to generate images from text prompts
- How to decode and save generated images
- Available image generation models
Basic usage
Section titled “Basic usage”import base64from openai import OpenAI
client = OpenAI()
prompt = """A background with server racks and cables, with a futuristic city skylinevisible through a large window at sunrise. The room has blue and green LEDlights illuminating the equipment."""
result = client.images.generate( model="gpt-image-2" , prompt=prompt, )
# Decode and save the imageimage_bytes = base64.b64decode(result.data[ 0].b64_json) with open( "generated_image.png" , "wb") as f: f.write(image_bytes)
print( "Image saved to generated_image.png" )curl -X POST "$OPENAI_BASE_URL /images/generations" \ -H "Authorization: Bearer $OPENAI_API_KEY " \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-image-2", "prompt": "A futuristic data center at sunrise with blue LED lights" }' Example models
Section titled “Example models”| Model (example) | Provider | Description |
|---|---|---|
gpt- | Azure | OpenAI’s image generation model |
Limitations and troubleshooting
Section titled “Limitations and troubleshooting”No image-generation model in the catalog runs on T Cloud. The examples use gpt-.
Next steps
Section titled “Next steps”- Multimodal — Analyze images with vision models
- Queued requests — Wait for rate-limit budget instead of getting a 429
- API endpoints — Full endpoint reference