Image Generation and Editing
Level: Beginner · Category: API reference
Tags: Text-to-image · Image editing
POST https://openai.rc.asu.edu/v1/images/generations makes images from a text prompt, and
POST /v1/images/edits changes an image you upload, in the OpenAI Images API shape. Keys and setup
are on LLM API Access.
| Model | Notes |
|---|---|
flux-2 | FLUX.2 [dev], 32B. Text-to-image and edits, including edits guided by several reference images. |
qwen-image-21 | Qwen-Image 2.1. Text-to-image and edits; licensed for non-commercial research use only. |
Check each model's license (in its description in /v1/models or Voyager) before using images
outside research.
Generate an image
- Python
- curl
import base64
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY and OPENAI_BASE_URL
result = client.images.generate(
model="flux-2",
prompt="a saguaro cactus at sunrise, photorealistic",
size="1024x1024",
)
with open("saguaro.png", "wb") as f:
f.write(base64.b64decode(result.data[0].b64_json))
curl -s https://openai.rc.asu.edu/v1/images/generations \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "flux-2", "prompt": "a saguaro cactus at sunrise, photorealistic", "size": "1024x1024"}' \
| jq -r '.data[0].b64_json' | base64 -d > saguaro.png
- The image comes back base64-encoded in
data[0].b64_json(PNG). The response also has theseedthat was used. sizeisWIDTHxHEIGHT, for example512x512or1024x1024. Larger images take longer: in testing, a 512x512 image took about 30 seconds and a 1024x1024 image about a minute.- Set the client timeout to several minutes; when many people generate images at once, requests wait for a free accelerator (see Limits and errors).
Edit an image
Upload the image as a file with a prompt that describes the change:
import base64
from openai import OpenAI
client = OpenAI()
with open("saguaro.png", "rb") as image:
result = client.images.edit(
model="flux-2",
image=image,
prompt="add a full moon in the sky",
size="1024x1024",
)
with open("saguaro-moon.png", "wb") as f:
f.write(base64.b64decode(result.data[0].b64_json))
Edits are multipart uploads and aren't retried automatically, so retry in your own code if one fails
with a 502 or 503.
Images inside a chat
Some chat models can also draw during a conversation: the gateway gives them a generate_image tool
(the image_generation add-on, see Gateway add-ons), and the image is
appended to the reply as a Markdown data URL. For scripts, call /v1/images/generations directly.
Related guides
- LLM API Access: keys, the endpoint, and every other endpoint.
- Video generation: short clips from a prompt or a first frame.