§15.16

Generate Images with the OpenAI API (gpt-image)

Two ways to make an image with the OpenAI API — the Images endpoint and the image_generation tool — save the PNG, set size, quality and format.

published 06 Sept 2026 checked against docs 06 Sept 2026 3 min in Building with the API Markdown

On this page5 sections
  1. 1. Key
  2. 2. Images API: prompt in, PNG out
  3. 3. Responses API: the model generates as a tool
  4. 4. The knobs
  5. 5. Edits

There are two doors to the same models. The Images API is the direct one: prompt in, image out. The Responses API lets a chat model decide to call an image_generation tool as part of a conversation, so you can refine an image in follow-up turns.

1. Key

export OPENAI_API_KEY="sk-..."   # macOS/Linux; PowerShell: $env:OPENAI_API_KEY="sk-..."
pip install openai

Both SDK calls below read the key from the environment.

2. Images API: prompt in, PNG out

from openai import OpenAI
import base64

client = OpenAI()

result = client.images.generate(
    model="gpt-image-2",
    prompt="A children's book drawing of a veterinarian using a stethoscope to listen to the heartbeat of a baby otter.",
)

with open("otter.png", "wb") as f:
    f.write(base64.b64decode(result.data[0].b64_json))

Returns the image as base64 in data[0].b64_json; decoding it and writing the bytes gives you a PNG. gpt-image-2 is the current model; gpt-image-1.5, gpt-image-1 and gpt-image-1-mini are the cheaper/older options.

3. Responses API: the model generates as a tool

from openai import OpenAI
import base64

client = OpenAI()

response = client.responses.create(
    model="gpt-6-astra",
    input="Generate an image of a gray tabby cat hugging an otter with an orange scarf",
    tools=[{"type": "image_generation"}],
)

images = [o.result for o in response.output if o.type == "image_generation_call"]
if images:
    with open("otter.png", "wb") as f:
        f.write(base64.b64decode(images[0]))

The text model plans the prompt and calls the tool; the image comes back as an image_generation_call output item with base64 in result. Continue the same conversation (“make the scarf blue”) to edit it.

4. The knobs

result = client.images.generate(
    model="gpt-image-2",
    prompt="Flat vector icon of a paper plane, single color, transparent background",
    size="1024x1024",        # or 1536x1024, 1024x1536, auto
    quality="high",          # low, medium, high, auto
    output_format="png",     # png (default), jpeg, webp
    background="transparent" # transparent, opaque, auto
)

n makes several images per call; output_compression (0–100) shrinks JPEG/WebP; moderation="low" relaxes the content filter — blocked requests come back as a moderation_blocked error.

5. Edits

The edits endpoint takes an input image (and optionally a mask marking the region to replace) plus a prompt, and returns the modified image in the same base64 shape. It’s the route for “same picture, change one thing”; for a new picture, generate again.


Next: your first OpenAI API request · generate images locally with FLUX.

← All Building with the API plates · Search all guides

↑↓ move↵ openalt+↵ copy first command

Keyboard

⌘/ctrl+K or /
Search all guides
alt+↵
In search: copy the guide's first command
j / k
Move through a list of guides
c
On a guide: copy its first command
t
Toggle light / dark
?
This list