豆包即梦3 seededit-3.0 本地图片编辑API接口文档
模型名称
seededit-3.0
API接口文档
豆包官方文档:https://www.volcengine.com/docs/82379/1666946
基础信息
- Base URL:
https://www.dmxapi.cn/v1/images/generations
- 请求方式: POST
- 认证方式: Bearer Token
请求参数
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
model | string | 是 | 使用的模型名称 |
prompt | string | 是 | 指导图像生成的文本描述 |
image | string | 是 | 输入图像的URL地址 |
seed | int | 否 | 随机种子(默认随机) |
guidance_scale | float | 否 | 提示词权重(默认5.5) |
size | string | 否 | 输出尺寸(默认"adaptive") |
watermark | bool | 否 | 是否添加水印(默认true) |
尺寸比例参考
1:1 → 1328x1328
16:9 → 1664x936
9:16 → 936x1664
3:4 → 1104x1472
Python调用示例
python
import base64
import os # 导入 os 库用于处理文件路径
import requests
# --- 将本地图片转换为符合 API 文档要求的 Base64 ---
# 1. 指定你的本地图片路径 (使用 r"" 避免转义问题)
image_path = r"c:\nezha1.png" # <------------------------- 改成你的图片路径,支持 JPEG 和 PNG 格式
url = "https://www.dmxapi.cn/v1/images/generations"
api_key = "sk-***************************************" # <--改成你的 DMXAPI 令牌
# 检查文件是否存在
if not os.path.exists(image_path):
print(f"错误:文件不存在,请检查路径是否正确: {image_path}")
exit()
# 2. 从文件名动态获取图片格式并确定MIME类型
file_extension = os.path.splitext(image_path)[1].lower()
if file_extension == ".png":
mime_type = "image/png"
elif file_extension in [".jpg", ".jpeg"]:
mime_type = "image/jpeg"
else:
# 如果是不支持的格式,打印错误并退出
print(f"错误:不支持的图片格式 '{file_extension}'。API仅支持 jpeg 和 png。")
exit()
# 检查文件大小是否超过10MB (10 * 1024 * 1024 bytes)
if os.path.getsize(image_path) > 10 * 1024 * 1024:
print("错误:图片大小超过10MB限制。")
exit()
base64_image = ""
try:
with open(image_path, "rb") as image_file:
# 3. 读取图片文件二进制数据,并进行 Base64 编码
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
# 4. 严格按照 API 文档格式构造成 Data URI
# 格式: data:<MIME类型>;base64,<Base64编码>
base64_image = (
f"data:{mime_type};base64,{encoded_string}" # 必须用这个格式输入 base64
)
except Exception as e:
print(f"读取或编码图片时发生错误: {e}")
exit()
# ------------------------------------
# 设置请求头
headers = {
"Authorization": f"Bearer {api_key}"
} # Content-Type 会由 requests 的 json 参数自动设置
# 构造请求的 JSON 数据体 (payload)
payload = {
"model": "seededit-3.0",
"prompt": "给哪吒带上海岛独眼龙",
"image": base64_image, # 5. 在 "image" 参数里使用格式化后的 Base64 字符串
"seed": 123,
"guidance_scale": 5.5,
"size": "adaptive",
"watermark": True,
}
if base64_image:
try:
# 6. 发送 POST 请求 (使用 json=payload 是更推荐的方式)
response = requests.post(url, headers=headers, json=payload)
# 如果状态码不是 2xx,将抛出 HTTPError 异常
response.raise_for_status()
imagesResponse = response.json()
if (
imagesResponse
and imagesResponse.get("data")
and len(imagesResponse["data"]) > 0
):
print("成功生成图片,URL 如下:")
print(imagesResponse["data"][0].get("url", "URL未在响应中提供"))
else:
print("API成功响应,但未能获取图片URL。API返回内容:")
print(imagesResponse)
except requests.exceptions.HTTPError as e:
# 关键:打印出服务器返回的具体错误信息,帮助定位问题
print(f"请求失败,HTTP 错误: {e}")
print(f"状态码: {e.response.status_code}")
print(f"服务器响应内容: {e.response.text}")
except requests.exceptions.RequestException as e:
print(f"请求发生网络或连接错误: {e}")
except Exception as e:
print(f"发生未知错误: {e}")
注意
返回结果里的 url
需要转义, 把 \\u0026
替换成 &
即可正常访问。
错误处理
常见错误码:
- 401 - 认证失败
- 400 - 参数错误
- 429 - 请求过于频繁
- 500 - 服务器内部错误