Docs / 图像生成
图像生成
通过兼容 OpenAI 的 POST /v1/images/generations 端点,从文字提示词生成图像。图像在同一次响应中返回:GPT Image 系列以 base64 数据返回,Seedream 4.5 返回下载链接,无需轮询。
#模型
| 模型 ID | 系列 | 说明 |
|---|---|---|
| gpt-image-2 | OpenAI | 原生支持最高 4K,图像输出免费 |
| gpt-image-2.6 | OpenAI | 静态图、GIF 动图与 SVG,透明背景,图生图;每张 $0.04 |
| seedream-4.5 | ByteDance Seedream | 2K 与 4K 图像,参考图生图,一次生成一组相关图片;每张 $0.05 |
在 RelayDance 上图像输出免费: 你只需为输入付费。支持人脸与人像,任务立即开始且无排队。
gpt-image-2.6 则按张计费:每返回一张图固定 $0.04,与尺寸和格式无关。它新增了 GIF 动图与 SVG 输出、透明背景和图生图,详见下文的 GPT Image 2.6。
seedream-4.5 同样按张计费:每生成一张收 $0.05,2K 与 4K 同价。参考图不收费;一次生成一组相关图片时,按实际返回的张数每张 $0.05。详见下文的 Seedream 4.5。
#API 用法
cURL
curl https://relaydance.com/v1/images/generations \
-H "Authorization: Bearer $RELAYDANCE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A serene Japanese garden with cherry blossoms, watercolor style",
"n": 1
}' | jq -r '.data[0].b64_json' | base64 --decode > garden.pngPython
import base64, os, requests
response = requests.post(
"https://relaydance.com/v1/images/generations",
headers={
"Authorization": f"Bearer {os.environ['RELAYDANCE_API_KEY']}",
"Content-Type": "application/json",
},
json={
"model": "gpt-image-2",
"prompt": "A serene Japanese garden with cherry blossoms, watercolor style",
"n": 1,
},
timeout=300,
)
response.raise_for_status()
image = response.json()["data"][0]
with open("garden.png", "wb") as f:
f.write(base64.b64decode(image["b64_json"]))OpenAI SDK
import fs from "node:fs";
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.RELAYDANCE_API_KEY,
baseURL: "https://relaydance.com/v1",
});
const res = await client.images.generate({
model: "gpt-image-2",
prompt: "A futuristic city at dusk, cinematic lighting",
n: 1,
});
const b64 = res.data?.[0]?.b64_json;
if (b64) fs.writeFileSync("city.png", Buffer.from(b64, "base64"));#响应
{
"created": 1794605056,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAA...",
"revised_prompt": "A serene Japanese garden with cherry blossoms, watercolor style"
}
]
}使用 GPT Image 系列时,图像以 base64 形式内嵌在 data[].b64_json 中:解码后把字节写入文件即可。它们不提供托管的图片链接,请不要对它们依赖 response_format: "url"。Seedream 4.5 不同:默认返回下载链接,见下文的响应说明。
#请求参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 图像模型 ID |
prompt | string | 要生成图像的文字描述 |
n | integer | 要生成的图像数量。默认: 1 |
size | string | 可选的输出尺寸,取决于模型(如 "1024x1024") |
#GPT Image 2.6:静态图、GIF 与 SVG
gpt-image-2.6 使用同一个端点,并新增了多种输出:除 PNG、JPEG、WebP 外,还可以返回 GIF 动图或 SVG,支持透明背景,也可以在已有图片上做修改。每返回一张图固定收取 $0.04,与尺寸、质量档位和格式无关。请求 n 张就按 n 张计费,失败的请求不扣费。
静态图
curl https://relaydance.com/v1/images/generations \
-H "Authorization: Bearer $RELAYDANCE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2.6",
"prompt": "A paper kite flying over green hills, flat illustration",
"size": "1024x1024"
}' | jq -r '.data[0].b64_json' | base64 --decode > kite.pngGIF 动图
把 output_format 设为 gif,并在提示词里写清动作即可。价格同样是 $0.04。
curl https://relaydance.com/v1/images/generations \
-H "Authorization: Bearer $RELAYDANCE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2.6",
"prompt": "A candle flame flickering, simple flat illustration",
"output_format": "gif"
}' | jq -r '.data[0].b64_json' | base64 --decode > candle.gif帧数、帧间隔和画面尺寸都由服务自行决定。实测返回的是循环播放的动画,共 8 到 9 帧,每帧约 160 到 350 毫秒。画面较小,每帧总像素约 17 万到 19 万(例如 414x413、508x337 或 217x883),size 只会影响大致的宽高比。不支持 frame_count 与 frame_duration_ms,传了也会被忽略。
Python
下面的示例按响应里给出的格式决定文件扩展名,返回什么格式就存成什么格式。
import base64, os, requests
resp = requests.post(
"https://relaydance.com/v1/images/generations",
headers={"Authorization": f"Bearer {os.environ['RELAYDANCE_API_KEY']}"},
json={
"model": "gpt-image-2.6",
"prompt": "A candle flame flickering, simple flat illustration",
"output_format": "gif", # png(默认)、jpeg、webp、gif 或 svg
"n": 1,
},
timeout=300,
)
resp.raise_for_status()
body = resp.json()
ext = body.get("output_format") or "png"
for i, item in enumerate(body["data"]):
with open(f"image_{i}.{ext}", "wb") as f:
f.write(base64.b64decode(item["b64_json"]))
print(f"saved image_{i}.{ext}", item.get("width"), item.get("height"))图生图(编辑已有图片)
POST /v1/images/edits 接收 multipart 表单而不是 JSON:源图片放在 image 文件字段里,另外带上 model 与 prompt。这里同样可以用 output_format(加上 -F output_format=gif 就能把一张静态图变成动图)。价格相同:每返回一张 $0.04。
curl https://relaydance.com/v1/images/edits \
-H "Authorization: Bearer $RELAYDANCE_API_KEY" \
-F model=gpt-image-2.6 \
-F [email protected] \
-F prompt="Turn this picture into a watercolor painting" \
| jq -r '.data[0].b64_json' | base64 --decode > edited.png响应
{
"created": 1794604905,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAA...",
"revised_prompt": "A paper kite flying over green hills, flat illustration",
"width": 1024,
"height": 1024
}
],
"output_format": "png",
"size": "1024x1024",
"quality": "auto",
"background": "auto",
"usage": { "input_tokens": 10, "output_tokens": 1056, "total_tokens": 1066 }
}width 与 height 是返回文件的真实像素尺寸,output_format 是实际产出的格式。usage 仅供参考:计费按张,不按 token。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | gpt-image-2.6 |
prompt | string | 要画的内容。生成 GIF 时请同时描述动作 |
n | integer | 生成数量,默认 1。每返回一张都会计费 |
size | string | 1024x1024、1536x1024、1024x1536、2048x2048,或自定义 宽x高,例如 1280x720、1920x1088。宽和高都必须是 16 的倍数,总像素须在 655,360 到 8,294,400 之间,因此 512x512 与 1920x1080 会被拒绝。不传或传 auto 则由服务自行决定 |
output_format | string | png(默认)、jpeg、webp、gif 或 svg |
background | string | 设为 transparent 可得到透明背景,请配合 png 或 webp 使用 |
quality | string | 为兼容而接受(low、medium、high),不影响价格 |
image | file | 仅用于编辑端点:源图片,以 multipart 文件字段上传 |
response_format | string | 不支持:图像始终以 base64 形式放在 b64_json 中返回 |
frame_count, frame_duration_ms | integer | 不支持:会被忽略,GIF 的帧数与帧间隔由服务决定 |
耗时与限制
- 静态图约需 20 到 45 秒,GIF 约 30 到 55 秒,SVG 约 90 秒。请把 HTTP 客户端的超时时间设为至少 180 秒。
- 调用是同步的:图像就在本次请求的响应里。没有流式输出、没有中间预览图,也没有批量模式。
- 文件扩展名请以响应里的
output_format字段或文件开头的字节为准,而不是以你请求的格式为准。 - 聊天类客户端也能用:
gpt-image-2.6同样可以走POST /v1/chat/completions,回复内容是一段 Markdown 图片,图片以 data URI 形式内嵌。每次回复同样收取 $0.04。
#Seedream 4.5:2K 与 4K 图像、参考图生图与组图
seedream-4.5 是字节跳动的 Seedream 4.5,使用同一个 JSON 端点。它可以生成 2K 或 4K 图像,可以基于一张或多张参考图作画,也可以一次返回一组相关的图片。每生成一张收取 $0.05,与尺寸无关;参考图不收费。失败的请求不扣费,包括被标准内容审核拒绝的提示词或结果。
文生图
响应里给的是下载链接,所以示例把它交给第二个 curl 保存成 JPEG 文件。
curl https://relaydance.com/v1/images/generations \
-H "Authorization: Bearer $RELAYDANCE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "seedream-4.5",
"prompt": "A lighthouse on a rocky coast at golden hour, oil painting",
"size": "2K"
}' | jq -r '.data[0].url' | xargs curl -s -o lighthouse.jpg参考图生图
把参考图放在 image 字段:一个 URL、一个 URL 数组,或 data:image/png;base64,... 这样的字符串。在提示词里用 image 1、image 2 等依次指代它们。URL 必须能被公网下载:如果对方站点拒绝下载(例如返回 403),请求会失败。这个模型没有 multipart 的编辑端点,图生图一律走 POST /v1/images/generations。
import os, requests
resp = requests.post(
"https://relaydance.com/v1/images/generations",
headers={"Authorization": f"Bearer {os.environ['RELAYDANCE_API_KEY']}"},
json={
"model": "seedream-4.5",
"prompt": "The cat from image 2 wearing the red hat from image 1, sitting on a windowsill, photo",
# 公网可下载的图片 URL,或 data:image/...;base64,... 字符串
"image": ["https://example.com/hat.jpg", "https://example.com/cat.jpg"],
"size": "2K",
},
timeout=300,
)
resp.raise_for_status()
item = resp.json()["data"][0]
with open("cat.jpg", "wb") as f:
f.write(requests.get(item["url"], timeout=120).content)
print("saved cat.jpg", item["size"])一次生成一组图片
把 sequential_image_generation 设为 auto,再用 sequential_image_generation_options.max_images(1 到 15)限定张数上限。模型按提示词决定生成几张,不超过上限,按实际返回的张数每张收 $0.05。这个模型会忽略 n:不传这两个字段时,每次调用只返回一张。
import os, requests
resp = requests.post(
"https://relaydance.com/v1/images/generations",
headers={"Authorization": f"Bearer {os.environ['RELAYDANCE_API_KEY']}"},
json={
"model": "seedream-4.5",
"prompt": "A set of three illustrations of the same red fox: in a spring meadow, in a summer forest, in autumn leaves",
"sequential_image_generation": "auto",
"sequential_image_generation_options": {"max_images": 3},
"size": "2K",
},
timeout=300,
)
resp.raise_for_status()
for i, item in enumerate(resp.json()["data"]):
if "url" in item:
with open(f"fox_{i}.jpg", "wb") as f:
f.write(requests.get(item["url"], timeout=120).content)
print(f"saved fox_{i}.jpg", item["size"])响应
{
"model": "seedream-4-5-251128",
"created": 1795612345,
"data": [
{
"url": "https://...",
"size": "2304x1728"
}
],
"usage": { "generated_images": 1, "output_tokens": 15552, "total_tokens": 15552 }
}data[].url 是直接下载链接,24 小时内有效,请及时下载保存。传 response_format: "b64_json" 则改为在 data[].b64_json 中返回 base64。size 是每张图的真实像素尺寸,usage.generated_images 就是本次计费的张数。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | seedream-4.5 |
prompt | string | 要画的内容。传了参考图时,用 image 1、image 2 等指代它们 |
size | string | 传 2K 或 4K 时,宽高比由模型按提示词决定(例如 2K 出 2304x1728 或 2848x1600,4K 出 4704x3520)。要固定形状就传 宽x高,总像素不少于 3,686,400,例如 2048x2048 或 2560x1440。更小的尺寸(如 1024x1024)会被拒绝 |
image | string or array | 参考图:公网 URL,或 data:image/...;base64,... 字符串。不计费 |
sequential_image_generation | string | 传 auto 允许生成一组相关图片;默认 disabled,只出一张 |
sequential_image_generation_options | object | {"max_images": 1 到 15}:一组最多几张 |
response_format | string | url(默认,链接 24 小时内有效)或 b64_json |
watermark | boolean | 默认为 false:返回的图片不带水印。传 true 会在右下角加一个小的 "AI generated" 标记 |
n | integer | 会被忽略:要多张请用 sequential_image_generation |
耗时与限制
- 一张 2K 图约需 6 到 17 秒;一张 4K 图或一组三张约需 45 秒。请把 HTTP 客户端的超时时间设为至少 180 秒。
- 调用是同步的:图片就在本次请求的响应里,没有需要轮询的任务。
- 提示词、参考图和生成结果都会经过标准内容审核。被拒绝的请求会返回错误,不扣费。
#获得更好效果的建议
- 具体描述: 在提示词中描述风格、构图、光线和氛围
- 指定风格: 像 "photorealistic"、"watercolor"、"anime" 这样的关键词能强烈影响输出
- 反复迭代: 提示词的细微改动可能大幅改变结果; 生成、调整、再生成