Image Generation
This article explains how to generate and edit images via the /v1/images/generations and /v1/images/edits endpoints, covering:
- Doubao Seedream (
/v1/images/generations, OpenAI-compatible;/v1/images/editsnot yet supported) - OpenAI GPT-image-2 / GPT-image-1 (Azure,
/v1/images/generationsfor text-to-image +/v1/images/editsfor image-to-image, with native transparent background support) - Gemini Nano Banana (uses
/v1/chat/completions, supports multi-image editing and streaming) — see Gemini Nano Banana
Full model list and pricing: Image Generation Model List.
Prerequisites
1. Install the latest OpenAI Python SDK
pip install --upgrade openai
2. Prepare the base64 module
Included in the Python 3 standard library; no additional installation required.
Example 1: Generate an Image
from openai import OpenAI
import base64
client = OpenAI(
api_key="your-api-key",
base_url="https://live-turing.cn.llm.tcljd.com/api/v1",
)
img = client.images.generate(
prompt="a futuristic city with flying cars and neon lights", # Image description
model="doubao-seedream-4-5-251128", # Model to use
n=1,
size="1024x1024",
)
image_bytes = base64.b64decode(img.data[0].b64_json)
with open("output.png", "wb") as f:
f.write(image_bytes)
print("Image successfully generated and saved as output.png")
Example 2: Edit Multiple Reference Images
Provide several reference images along with a text description; the model outputs one composite image.
import base64
from openai import OpenAI
client = OpenAI(
api_key="your-api-key",
base_url="https://live-turing.cn.llm.tcljd.com/api/v1",
)
prompt = """
Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.
"""
result = client.images.edit(
model="turing/gpt-image-2",
n=1,
size="1024x1024",
image=[
open("body-lotion.png", "rb"),
open("bath-bomb.png", "rb"),
open("incense-kit.png", "rb"),
open("soap.png", "rb"),
],
prompt=prompt,
timeout=120, # Complex image generation may be slow; increasing the timeout is recommended
)
image_bytes = base64.b64decode(result.data[0].b64_json)
with open("gift-basket.png", "wb") as f:
f.write(image_bytes)
print("Image successfully edited and saved as gift-basket.png")
Example 3: Edit a Single Image
import base64
from openai import OpenAI
client = OpenAI(
api_key="your-api-key",
base_url="https://live-turing.cn.llm.tcljd.com/api/v1",
)
result = client.images.edit(
model="turing/gpt-image-2",
n=1,
size="1024x1024",
image=open("body-lotion.png", "rb"),
prompt="Replace the color of the bottle with blue",
timeout=120,
)
image_bytes = base64.b64decode(result.data[0].b64_json)
with open("blue-bottle.png", "wb") as f:
f.write(image_bytes)
print("Image successfully edited and saved as blue-bottle.png")
Seedream Batch Generation
Seedream's n parameter is automatically converted to batch generation parameters:
# When n=3, the platform automatically adds:
# "sequential_image_generation": "auto"
# "sequential_image_generation_options": {"max_images": 3}
client.images.generate(
model="doubao-seedream-4-5-251128",
prompt="...",
n=3,
)
"auto": The model automatically decides whether to return multiple images and how many"disabled": Generate only one image
Gemini Nano Banana
Gemini family image generation models use the standard /chat/completions endpoint (not /images/generations), controlling output via modalities: ["text", "image"] and imageConfig.
Supported Models
The default RPH (requests per hour) is 60. For available models, pricing, and specifications, refer to the Image Model List → Gemini Nano Banana.
Basic Example
curl -N $TURING_BASE_URL/chat/completions \
-H "Authorization: Bearer $TURING_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "turing/gemini-3-pro-image",
"stream": true,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Generate a Banana with saying hello"}
],
"modalities": ["text", "image"],
"imageConfig": {
"aspectRatio": "1:1",
"imageSize": "1K",
"imageOutputOptions": {"mimeType": "image/jpeg", "compressionQuality": 95}
}
}'
Three Input Forms
Text only
{
"model": "$model",
"stream": true,
"messages": [
{"role": "user", "content": "Generate a Banana with saying hello"}
]
}
Multi-turn with images (image editing)
{
"model": "$model",
"stream": true,
"messages": [
{"role": "user", "content": [{"type": "text", "text": "Please draw me a dog"}]},
{"role": "assistant", "content": [
{"type": "text", "text": "Here you go!"},
{"type": "image_url", "image_url": {"url": "{image_url}", "detail": "low"}}
]},
{"role": "user", "content": [{"type": "text", "text": "Add a hat on the dog"}]}
]
}
Control aspect ratio and resolution
{
"model": "$model",
"stream": false,
"messages": [{"role": "user", "content": "Please draw a cute dog"}],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "1K",
"imageOutputOptions": {"mimeType": "image/jpeg", "compressionQuality": 95}
}
}
Supported Aspect Ratios and Resolutions
Gemini 2.5 Flash Image
| Aspect ratio | Resolution | Tokens |
|---|---|---|
| 1:1 | 1024x1024 | 1290 |
| 2:3 | 832x1248 | 1290 |
| 3:2 | 1248x832 | 1290 |
| 3:4 | 864x1184 | 1290 |
| 4:3 | 1184x864 | 1290 |
| 4:5 | 896x1152 | 1290 |
| 5:4 | 1152x896 | 1290 |
| 9:16 | 768x1344 | 1290 |
| 16:9 | 1344x768 | 1290 |
| 21:9 | 1536x672 | 1290 |
Gemini 3 Pro Image / Gemini 3.1 Flash Image
| Aspect ratio | 1K resolution | 1K Tokens | 2K resolution | 2K Tokens | 4K resolution | 4K Tokens |
|---|---|---|---|---|---|---|
| 1:1 | 1024x1024 | 1210 | 2048x2048 | 1210 | 4096x4096 | 2000 |
| 2:3 | 848x1264 | 1210 | 1696x2528 | 1210 | 3392x5056 | 2000 |
| 3:2 | 1264x848 | 1210 | 2528x1696 | 1210 | 5056x3392 | 2000 |
| 3:4 | 896x1200 | 1210 | 1792x2400 | 1210 | 3584x4800 | 2000 |
| 4:3 | 1200x896 | 1210 | 2400x1792 | 1210 | 4800x3584 | 2000 |
| 4:5 | 928x1152 | 1210 | 1856x2304 | 1210 | 3712x4608 | 2000 |
| 5:4 | 1152x928 | 1210 | 2304x1856 | 1210 | 4608x3712 | 2000 |
| 9:16 | 768x1376 | 1210 | 1536x2752 | 1210 | 3072x5504 | 2000 |
| 16:9 | 1376x768 | 1210 | 2752x1536 | 1210 | 5504x3072 | 2000 |
| 21:9 | 1584x672 | 1210 | 3168x1344 | 1210 | 6336x2688 | 2000 |
Response Format
Streaming (gemini-2.5-flash-image): Images are returned as image_url blocks inside content.
Streaming Beta (gemini-3-pro-image / gemini-3.1-flash-image / gemini-3.1-flash-lite-image): Images are returned in a separate images field; content is an empty array.
{
"choices": [{
"delta": {
"content": [],
"images": [{"type": "image_url", "image_url": {"url": "{Base64 string}"}}]
}
}]
}
Non-streaming (gemini-2.5-flash-image):
{
"choices": [{
"message": {
"role": "assistant",
"content": [
{"type": "text", "text": "Here's your dog!"},
{"type": "image_url", "image_url": {"url": "{generated_image_url}", "detail": "low"}}
]
},
"finish_reason": "stop"
}]
}
Non-streaming Beta (gemini-3-pro-image / gemini-3.1-flash-image / gemini-3.1-flash-lite-image): Images are in the choices[*].images field.
Capabilities
- Image generation: Generate images from prompts
- Streaming output: Supports real-time streaming responses
- Image editing: Pass historical images in a multi-turn conversation to incrementally modify existing images
GPT-image-2
turing/gpt-image-2 is the latest generation image model (public preview) provided by OpenAI via Azure. Compared to gpt-image-1, it offers:
- Support for arbitrary resolutions (4K, long side ≤ 3840 px, aspect ratio ≤ 3:1)
- Redesigned quality control (
lowoptimized for latency) - Native transparent background
Two endpoints are supported:
POST /v1/images/generations(JSON body) — text-to-imagePOST /v1/images/edits(multipart/form-data) — image-to-image / image editing
Parameter Overview
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
model | string | Yes | - | Fixed value: turing/gpt-image-2 |
prompt | string | Yes | - | Text description, up to 32000 characters |
n | int | No | 1 | Number of images to return per request, 1–10 |
size | string | No | "auto" | "auto" or <w>x<h>: both dimensions must be multiples of 16; long side ≤ 3840; aspect ratio ≤ 3:1; total pixels 655,360–8,294,400 |
quality | string | No | "high" | "low" / "medium" / "high"; low optimizes for latency |
output_format | string | No | "png" | "png" / "jpeg" (Azure does not yet support webp) |
output_compression | int | No | 100 | 0–100; only applies to jpeg |
background | string | No | "auto" | "transparent" / "opaque" / "auto"; transparent requires output_format="png" |
moderation | string | No | "auto" | "auto" / "low"; low applies more permissive content moderation |
user | string | No | - | End-user identifier for auditing |
response_format: GPT-image models always return base64 (b64_json);urlis not supportedstyle: Only supported bydall-e-3
Text-to-Image
POST directly to /v1/images/generations with a JSON request body.
curl $TURING_BASE_URL/images/generations \
-H "Authorization: Bearer $TURING_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "turing/gpt-image-2",
"prompt": "a close-up of a bear walking through a misty forest at dawn",
"n": 1,
"size": "1536x1024",
"quality": "high",
"output_format": "png"
}' \
| jq -r '.data[0].b64_json' | base64 -d > bear.png
Response example:
{
"created": 1729753028,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANS..."
}
],
"usage": {
"input_tokens": 50,
"input_tokens_details": { "text_tokens": 50, "image_tokens": 0 },
"output_tokens": 1568,
"total_tokens": 1618
}
}
4K and Arbitrary Resolutions
gpt-image-2 is no longer limited to the three fixed sizes 1024x1024 / 1024x1536 / 1536x1024. You can specify any custom <w>x<h>, such as 3840x2160 (4K landscape) or 2160x3840 (4K portrait). Simply replace the size field in the request body above with the desired dimensions:
{
"model": "turing/gpt-image-2",
"prompt": "cyberpunk Tokyo street, neon reflections on wet asphalt, 4k cinematic",
"size": "3840x2160",
"quality": "high"
}
Constraints (enforced):
width % 16 == 0 and height % 16 == 0max(width, height) <= 3840max(w/h, h/w) <= 3655_360 <= width * height <= 8_294_400
If these are not met, the API returns 4xx immediately. It is recommended to validate on the client side before calling.
Transparent Background
To request a transparent background, set both "background": "transparent" and "output_format": "png" in the request body:
curl $TURING_BASE_URL/images/generations \
-H "Authorization: Bearer $TURING_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "turing/gpt-image-2",
"prompt": "a single red maple leaf, isolated",
"size": "1024x1024",
"background": "transparent",
"output_format": "png"
}'
Image Editing (Image-to-Image)
Endpoint: POST /v1/images/edits, request body is multipart/form-data.
Single image input
curl $TURING_BASE_URL/images/edits \
-H "Authorization: Bearer $TURING_API_KEY" \
-F "model=turing/gpt-image-2" \
-F "prompt=Replace the background with a beach sunset scene" \
-F "image=@input.png" \
-F "n=1" \
-F "size=1024x1024" \
-F "quality=medium" \
| jq -r '.data[0].b64_json' | base64 -d > output.png
Multiple reference images (use image[] as the field name)
curl $TURING_BASE_URL/images/edits \
-H "Authorization: Bearer $TURING_API_KEY" \
-F "model=turing/gpt-image-2" \
-F "prompt=Generate a gift basket containing all items in the reference images" \
-F "image[]=@item1.png" \
-F "image[]=@item2.png" \
-F "image[]=@item3.png" \
-F "n=1" \
-F "size=1024x1024" \
-F "quality=medium" \
| jq -r '.data[0].b64_json' | base64 -d > output.png
With mask (inpainting)
Provide a mask (must be PNG; transparent pixels indicate the area to edit). The model only repaints the transparent region:
curl $TURING_BASE_URL/images/edits \
-H "Authorization: Bearer $TURING_API_KEY" \
-F "model=turing/gpt-image-2" \
-F "prompt=Add an orange cat in the transparent area" \
-F "image=@input.png" \
-F "mask=@mask.png" \
-F "size=1024x1024" \
-F "quality=high" \
| jq -r '.data[0].b64_json' | base64 -d > output.png
Input images support PNG / JPG / JPEG; masks only support PNG. Individual files must not exceed 50 MB.
Image editing-specific parameters
| Parameter | Type | Description |
|---|---|---|
image | file | Input image (single); for multiple images, use image[] repeated |
mask | file | Optional PNG mask; transparent area indicates the region to edit |
input_fidelity | string | "low" / "high", controls how closely the input image is preserved |
output_format | string | "png" / "jpeg" |
background | string | "auto" / "transparent" (requires output_format=png) |
Errors and Timeouts
| Scenario | Behavior |
|---|---|
| Rate limit exceeded | HTTP 429; exponential backoff retry recommended |
| Prompt triggers content moderation | HTTP 4xx, error.code = "contentFilter" |
| Output image triggers content moderation | HTTP 4xx, error.message indicates Generated image was filtered ... |
| Single generation duration | Typically 120 seconds; complex 4K + high quality may reach 180–240 seconds; setting timeout >= 300s is recommended |
Full field definitions and Try-It: /api/create-image.
See also
/api/create-image— Complete schema, all supported parameters, Try-It- Image Generation Model List — Per-model pricing, resolutions, and deprecation dates
- Gemini Nano Banana — Gemini image generation (uses
/v1/chat/completions, supports multi-image editing + streaming) - Video Generation — Video generation API