功能API 端点说明文生图/v1/image_generation从文本描述生成图片图生图/v1/image_generation基于参考图 + 文本生成新图
text
文本描述 → API 调用 → 自动保存本地 → 返回路径
text
参考图 + 文本描述 → API 调用 → 自动保存本地 → 返回路径
python
import requests
import os
import base64
API_KEY = os.getenv("MINIMAX_API_KEY")
URL = "https://api.minimaxi.com/v1/image_generation"
payload = {
"model": "image-01",
"prompt": "一只可爱的小老虎在森林里",
"aspect_ratio": "16:9",
"n": 1,
"response_format": "url"
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(URL, json=payload, headers=headers)
result = response.json()
image_url = result["data"]["image_urls"][0]
# 自动下载保存
save_dir = os.path.expanduser("~/.openclaw/workspace/assets/images")
os.makedirs(save_dir, exist_ok=True)
filename = f"generated_{int(time.time())}.jpeg"
save_path = os.path.join(save_dir, filename)
img_response = requests.get(image_url)
with open(save_path, 'wb') as f:
f.write(img_response.content)
print(f"图片已保存: {save_path}")
python
# 读取本地图片并转为 Base64
with open("reference.jpg", "rb") as f:
img_base64 = base64.b64encode(f.read()).decode()
payload = {
"model": "image-01",
"prompt": "same person wearing traditional Chinese Hanfu",
"aspect_ratio": "3:4",
"n": 1,
"subject_reference": [
{
"type": "character",
"image_file": f"data:image/jpeg;base64,{img_base64}"
}
]
}
模型说明适用场景image-01高质量图片生成通用场景image-01-live带风格的图片生成需要漫画/水彩等风格
参数类型必填说明modelstring是image-01 或 image-01-live``promptstring是图片描述,最长 1500 字符nint否生成数量 [1, 9],默认 1subject_referencearray否参考图配置(图生图用)styleobject否风格设置(仅 image-01-live)aspect_ratiostring否宽高比
字段类型必填说明typestring是当前仅支持 character``image_filestring是参考图 URL 或 Base64
值分辨率1:11024x102416:91280x7204:31152x8643:21248x8322:3832x12483:4864x11529:16720x128021:91344x576 (仅 image-01)
风格说明漫画漫画风格元气元气/活力风格中世纪中世纪风格水彩水彩画风格
生成的图片默认保存在: ~/.openclaw/workspace/assets/images/
文件名格式: {type}_{timestamp}_{index}.jpeg
type: text2img / img2imgtimestamp: Unix 时间戳index: 多图时的序号scripts/generate_image.py - 文生图工具(自动保存本地)scripts/image_to_image.py - 图生图工具(自动保存本地)bash
# 文生图
python generate_image.py "一只可爱的小老虎" --ratio 16:9 --n 3
# 图生图
python image_to_image.py "穿汉服的女孩" --ref path/to/image.jpg --ratio 3:4