Image Generation
Generate images from text prompts using the OpenAI-compatible Images API.
Prerequisites
- An API key (get one here)
- OpenAI SDK or HTTP client installed (Quickstart)
What you'll learn:
- How to generate images from text prompts
- How to decode and save generated images
- Available image generation models
Basic Usage
- Python
- curl
import base64
from openai import OpenAI
client = OpenAI()
prompt = """
A background with server racks and cables, with a futuristic city skyline
visible through a large window at sunrise. The room has blue and green LED
lights illuminating the equipment.
"""
result = client.images.generate(
model="gpt-image-1",
prompt=prompt,
)
# Decode and save the image
image_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
| Model | Provider | Description |
|---|---|---|
gpt-image-1 | Azure | OpenAI's image generation model |
Next Steps
- Multimodal — Analyze images with vision models
- Asynchronous Requests — Queue image generation for batch processing
- API Endpoints — Full endpoint reference