Seedream 5.0 Pro / Flash
Seedream 5.0 Pro 和 Flash 支持文生图、参考图编辑、多图融合、交互编辑和图层拆分。两款模型都使用 JSON 图片生成接口。
接口与模型
POST /v1/images/generations
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json| 模型 ID | 说明 |
|---|---|
doubao-seedream-5-0-pro | Pro,支持标准及快速提示词优化 |
doubao-seedream-5-0-flash | Flash,支持标准提示词优化 |
本页仅介绍 Pro 和 Flash。国内正式环境入口为 https://cubicspace.cn,使用该环境的 API Key。客户端使用上表中不带日期的模型 ID。
该接口是同步请求:一次 POST 直接返回图片结果,不返回需要轮询的任务 ID。普通生成返回一张图片;拆层返回底图和多个图层。
文生图
export CUBICSPACE_API_KEY="YOUR_API_KEY"
curl --max-time 420 https://cubicspace.cn/v1/images/generations \
-H "Authorization: Bearer $CUBICSPACE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "doubao-seedream-5-0-pro",
"prompt": "为咖啡品牌设计一张横向海报,暖色背景,清晰的中文标题",
"size": "1.5K",
"output_format": "png",
"response_format": "url",
"watermark": false
}'切换 Flash 时,将 model 改为 doubao-seedream-5-0-flash。可先用 1K 完成接入验证,再按需要调整到 1.5K 或 2K。
请求参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 上表中的模型 ID |
prompt | string | 生图必填;图层拆分时可省略,让模型自动识别元素 |
image | string / string[] | 参考图 URL 或 data:image/png;base64,...;普通生图最多 10 张,拆层必须且只能输入 1 张 |
size | string | 普通生图支持 1K、1.5K、2K,默认 2K;也支持符合限制的 宽x高 |
layer_decomposition | boolean | true 开启图层拆分,默认 false |
output_format | string | jpeg 或 png;拆层时只控制底图格式,独立图层始终为 PNG |
response_format | string | url(默认)或 b64_json |
watermark | boolean | 是否添加水印;显式 false 会保留 |
background | string | opaque 或 transparent;透明模式需要一张带透明通道的参考图,不能同时指定 JPEG 输出 |
optimize_prompt_options.mode | string | standard;Pro 另支持 fast |
普通生图指定 宽x高 时,总像素须在 921,600 至 4,624,220 之间,宽高比须在 1:16 至 16:1 之间。使用分辨率档位时,可在 prompt 中描述横竖构图或比例。
图层拆分的 size 支持 1K、1.5K、2K、auto,默认 auto;不支持自定义 宽x高。底图和每个图层的实际尺寸见响应的 data[].size。
size 的像素范围限制针对普通生成。拆出的独立元素可能远小于该范围,应以返回尺寸为准,不要强制将每个图层拉伸到整张底图大小。
Pro / Flash 不支持流式输出、组图参数或联网搜索。请省略 stream、sequential_image_generation、sequential_image_generation_options 和 tools。若携带 n,只能为 1。图生图也使用本页的 JSON 接口,参考图放在顶层 image。
参考图编辑和多图融合
在文生图请求中增加 image。单张参考图可直接传字符串,多图可传数组:
{
"model": "doubao-seedream-5-0-flash",
"prompt": "保留图一的人物,将服装改为图二的款式,保持人物姿势",
"image": ["https://example.com/person.png", "https://example.com/clothes.png"],
"size": "2K",
"response_format": "url"
}交互编辑时,可在提示词中结合坐标、框选或箭头标记说明修改区域;参考图仍通过 image 传入。
单张编辑可使用以下请求体:
{
"model": "doubao-seedream-5-0-flash",
"prompt": "保留构图和文字,只把蓝色圆形改成绿色",
"image": "https://example.com/poster.png",
"size": "1.5K",
"output_format": "png",
"response_format": "url",
"watermark": false
}example.com 地址为占位符,调用前请替换为可访问的真实图片地址。URL 不能指向本地文件、需要登录的网页或 HTML 预览页。
本地图片需要转换成 data URL,不能直接传文件路径,也不能只传裸 Base64:
import base64
from pathlib import Path
image = "data:image/png;base64," + base64.b64encode(
Path("reference.png").read_bytes()
).decode("ascii")普通参考图支持 JPEG、PNG、WebP、BMP、TIFF、GIF、HEIC、HEIF,单张不超过 30 MB、总像素不超过 3600 万,宽高均大于 14 像素,宽高比在 1:16 至 16:1 之间。拆层输入仅支持 PNG/JPEG,总像素至少 512×512、最多 3600 万,单张不超过 30 MB。
图层拆分
{
"model": "doubao-seedream-5-0-pro",
"image": "https://example.com/poster.png",
"layer_decomposition": true,
"size": "auto",
"output_format": "jpeg",
"response_format": "url"
}最多返回一张底图和 16 个独立图层。也可增加 prompt 指定需要拆分的元素。
Flash 也使用相同的拆层参数,只需替换模型 ID。任一图层生成失败时整个拆层请求报错,不按部分成功处理。
返回结果
以下为简化的图层拆分响应示例:
{
"model": "doubao-seedream-5-0-pro",
"created": 1784696685,
"data": [
{"url": "https://example.com/base.jpg", "size": "2048x2048", "output_format": "jpeg", "z_index": 0},
{
"url": "https://example.com/title.png",
"size": "1000x400",
"output_format": "png",
"z_index": 1,
"name": "标题",
"description": "海报顶部的标题文字",
"bounding_box": {"absolute": [100, 100, 600, 300], "normalized": [49, 49, 293, 146]}
}
],
"usage": {"input_images": 1, "generated_images": 2, "output_tokens": 100, "total_tokens": 100}
}普通生图也返回 data 数组。使用 b64_json 时,图片内容放在 data[].b64_json,无需从文本中提取图片。拆层的 z_index 表示叠放顺序,底图为 0;bounding_box 保留图层位置。图片 URL 通常在生成后 24 小时失效,请及时保存。
usage.input_images 是输入图片数量,usage.generated_images 是成功输出数量。拆层时输出数量包含底图;output_tokens 和 total_tokens 是本次请求返回的用量信息。上述 JSON 为结构示意,实际尺寸、层数和用量由响应决定。
还原图层位置
按 z_index 从小到大绘制。底图覆盖整张画布,通常不返回 bounding_box;独立图层使用自己的边界框:
absolute:[left, top, right, bottom],单位为底图像素。显示位置为(left, top),区域宽高为right-left和bottom-top。normalized:同样是四个边界值,范围为 0–1000。目标画布宽高为W、H时,位置是(left/1000×W, top/1000×H),区域大小是((right-left)/1000×W, (bottom-top)/1000×H)。- 将图层缩放到上述区域后叠放;保留 PNG 透明通道。
name和description用于标识内容,不能代替z_index排序。
图层文件的 size 不一定等于定位框宽高,甚至可能超过底图的某一边。请按 bounding_box 还原显示区域,不能直接把图层的文件像素尺寸作为画布上的显示尺寸。
Python 完整示例:调用并保存图片
以下示例使用 Python 标准库,无需安装 SDK。先设置环境变量 CUBICSPACE_API_KEY,再运行脚本。示例默认使用 Base64 返回;把 response_format 改为 url 可使用下载链接,保存代码支持两种方式。
import base64
import json
import os
from pathlib import Path
from urllib.error import HTTPError
from urllib.request import Request, urlopen
base_url = os.getenv("CUBICSPACE_BASE_URL", "https://cubicspace.cn").rstrip("/")
payload = {
"model": "doubao-seedream-5-0-flash",
"prompt": "奶油色背景的简洁海报,左侧红色纸船,右侧蓝色圆形,顶部标题 SPRING",
"size": "1K",
"output_format": "png",
"response_format": "b64_json",
"watermark": False,
}
# 单图编辑:增加 payload["image"] = "https://.../reference.png"
# 拆层:增加 image、layer_decomposition=True,并将 size 改为 "auto"。
# 拆层可删除 prompt,让模型自动识别主要元素。
request = Request(
base_url + "/v1/images/generations",
data=json.dumps(payload, ensure_ascii=False).encode("utf-8"),
headers={
"Authorization": "Bearer " + os.environ["CUBICSPACE_API_KEY"],
"Content-Type": "application/json",
},
method="POST",
)
try:
with urlopen(request, timeout=420) as response:
result = json.load(response)
except HTTPError as error:
raise RuntimeError(f"HTTP {error.code}: {error.read().decode('utf-8', errors='replace')}") from error
output = Path("seedream_output")
output.mkdir(exist_ok=True)
metadata = {**result, "data": []}
for index, item in enumerate(result["data"]):
if item.get("b64_json"):
content = base64.b64decode(item["b64_json"], validate=True)
else:
with urlopen(item["url"], timeout=90) as image_response:
content = image_response.read()
extension = item.get("output_format", "png")
path = output / f"image_{index:02d}.{extension}"
path.write_bytes(content)
metadata["data"].append({k: v for k, v in item.items() if k != "b64_json"})
print(path, item.get("size"), item.get("z_index"))
(output / "metadata.json").write_text(
json.dumps(metadata, ensure_ascii=False, indent=2), encoding="utf-8"
)拆层时务必遍历全部 data,只保存 data[0] 会遗漏独立图层。再次运行示例会覆盖同名输出文件,需要保留历史结果时请更换输出目录。
错误处理
| 情况 | 检查方法 |
|---|---|
| 401 / 鉴权失败 | 检查测试环境 API Key 与 Authorization: Bearer ... |
| 403 / 模型不可用 | 检查当前账户与令牌的模型权限 |
| 参数校验失败 | 检查 size、参考图数量、提示词和拆层组合;Flash 不使用 fast 提示词优化 |
| 图片读取失败 | 确保 URL 直接返回图片,或使用带 MIME 前缀的 data URL |
| 返回慢或客户端超时 | 给同步请求保留足够超时;超时不代表处理失败,不要循环重复提交 |
| 拆层位置错乱 | 按 z_index 排序,并按底图坐标解释 bounding_box |
不要对参数、权限或鉴权错误自动重试。排查时保留响应错误信息及请求 ID,避免记录或分享 API Key。