Generative AI
Beginner
4.5
Generate Images with the OpenAI API
Create images programmatically and save them to disk.
0h 20m
1 lesson
1.2K students
What You'll Learn
Learning objectives will be added soon.
Tutorial Content
A single call
from openai import OpenAI
import base64
client = OpenAI()
img = client.images.generate(
model="gpt-image-1",
prompt="a watercolor fox reading a book under a tree",
size="1024x1024",
)
data = base64.b64decode(img.data[0].b64_json)
open("fox.png", "wb").write(data)Prompting for images
Describe subject, style, composition, and lighting. "A watercolor fox, soft pastel palette, centered, warm afternoon light" beats "a fox." Generate a few variations and iterate — image prompting is as much craft as the text kind.
Your Progress
Sign in to track your progress
Tags
Diffusion
API
Python
Multimodal