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-1", 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-1", "prompt": "A futuristic data center at sunrise with blue LED lights" }'Available Models
Section titled “Available Models”| Model | Provider | Description |
|---|---|---|
gpt-image-1 | Azure | OpenAI’s image generation model |
Next Steps
Section titled “Next Steps”- Multimodal — Analyze images with vision models
- Asynchronous Requests — Queue image generation for batch processing
- API Endpoints — Full endpoint reference