Skip to main content

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/edits not yet supported)
  • OpenAI GPT-image-2 / GPT-image-1 (Azure, /v1/images/generations for text-to-image + /v1/images/edits for 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 ratioResolutionTokens
1:11024x10241290
2:3832x12481290
3:21248x8321290
3:4864x11841290
4:31184x8641290
4:5896x11521290
5:41152x8961290
9:16768x13441290
16:91344x7681290
21:91536x6721290

Gemini 3 Pro Image / Gemini 3.1 Flash Image

Aspect ratio1K resolution1K Tokens2K resolution2K Tokens4K resolution4K Tokens
1:11024x102412102048x204812104096x40962000
2:3848x126412101696x252812103392x50562000
3:21264x84812102528x169612105056x33922000
3:4896x120012101792x240012103584x48002000
4:31200x89612102400x179212104800x35842000
4:5928x115212101856x230412103712x46082000
5:41152x92812102304x185612104608x37122000
9:16768x137612101536x275212103072x55042000
16:91376x76812102752x153612105504x30722000
21:91584x67212103168x134412106336x26882000

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 (low optimized for latency)
  • Native transparent background

Two endpoints are supported:

  • POST /v1/images/generations (JSON body) — text-to-image
  • POST /v1/images/edits (multipart/form-data) — image-to-image / image editing

Parameter Overview

ParameterTypeRequiredDefaultDescription
modelstringYes-Fixed value: turing/gpt-image-2
promptstringYes-Text description, up to 32000 characters
nintNo1Number of images to return per request, 110
sizestringNo"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
qualitystringNo"high""low" / "medium" / "high"; low optimizes for latency
output_formatstringNo"png""png" / "jpeg" (Azure does not yet support webp)
output_compressionintNo1000100; only applies to jpeg
backgroundstringNo"auto""transparent" / "opaque" / "auto"; transparent requires output_format="png"
moderationstringNo"auto""auto" / "low"; low applies more permissive content moderation
userstringNo-End-user identifier for auditing
Unsupported Parameters
  • response_format: GPT-image models always return base64 (b64_json); url is not supported
  • style: Only supported by dall-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 == 0
  • max(width, height) <= 3840
  • max(w/h, h/w) <= 3
  • 655_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
File Limits

Input images support PNG / JPG / JPEG; masks only support PNG. Individual files must not exceed 50 MB.

Image editing-specific parameters

ParameterTypeDescription
imagefileInput image (single); for multiple images, use image[] repeated
maskfileOptional PNG mask; transparent area indicates the region to edit
input_fidelitystring"low" / "high", controls how closely the input image is preserved
output_formatstring"png" / "jpeg"
backgroundstring"auto" / "transparent" (requires output_format=png)

Errors and Timeouts

ScenarioBehavior
Rate limit exceededHTTP 429; exponential backoff retry recommended
Prompt triggers content moderationHTTP 4xx, error.code = "contentFilter"
Output image triggers content moderationHTTP 4xx, error.message indicates Generated image was filtered ...
Single generation durationTypically 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