Skip to content

Image Generation

This content is for v1.0.0. Switch to the latest version for up-to-date documentation.

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

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")

ModelProviderDescription
gpt-image-1AzureOpenAI’s image generation model