Image Generation with GPT-4
In this lesson, you'll learn how to use the image_generation tool in OpenAI’s API to generate images and save them to your local machine.
Prerequisites
Install the OpenAI SDK:
pip install openai
The Code
from openai import OpenAI
import base64
client = OpenAI()
response = client.responses.create(
model="gpt-4.1-mini",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[{"type": "image_generation"}],
)
# Save the image to a file
image_data = [
output.result
for output in response.output
if output.type == "image_generation_call"
]
if image_data:
image_base64 = image_data[0]
with open("cat_and_otter.png", "wb") as f:
f.write(base64.b64decode(image_base64))
Explanation
tools=[{"type": "image_generation"}]: Tells the model to produce an image.output.result: Contains the image in base64 format.base64.b64decode(...): Converts the image into binary and writes to a file.
Use Case
Great for:
- Artistic illustrations
- Generating story images or thumbnails
- Creative automation and visual storytelling