Generiert hochwertige Bilder mit xAI Grok/Flux API. Use when user asks for image generation ("mach a Bild von...", "generier PNG...", "Bild erstellen") or image editing ("ändere das Bild", "mach daraus..."). Handles Prompts, styles, aspect ratios, edits, batch generation. Outputs PNG via base64 or file.
API Key:$XAIAPIKEY(已配置)
保存目录:~/.openclaw/media/(解析为 /data/.openclaw/media/——允许用于Telegram发送)
bash
curl -s https://api.x.ai/v1/images/generations \
-H Authorization: Bearer $XAIAPIKEY \
-H Content-Type: application/json \
--data {
model: grok-imagine-image-pro,
prompt: <提示词>,
n: 1,
responseformat: b64json
} | python3 -c
import json, sys, base64, os, time
os.makedirs(os.path.expanduser(~/.openclaw/media), exist_ok=True)
r = json.load(sys.stdin)
ts = int(time.time())
for i, img in enumerate(r[data]):
imgdata = base64.b64decode(img[b64json])
fpath = os.path.expanduser(f~/.openclaw/media/generated{ts}{i}.png)
with open(fpath, wb) as f:
f.write(img_data)
print(fpath)
在JSON请求体中添加 aspect_ratio: <比例>。支持的数值:
| 比例 | 使用场景 |
|---|---|
| 1:1 | 社交媒体、缩略图 |
| 16:9 / 9:16 |
设置 n: <数量>(1-10)可在一次请求中生成多张图像。
通过提供源图像和编辑提示词来编辑现有图像。
使用相同的 /v1/images/generations 端点,额外添加 image_url 字段。
请勿使用 /v1/images/edits 配合 multipart 格式——xAI 要求使用 JSON。
重要提示: 对于本地文件,请使用 Python 构建 JSON 载荷文件,然后使用 @file 方式调用 curl。
在 curl 参数中内联 base64 编码会导致大于约100KB的图像出现参数列表过长错误。
注意: 这不是真正的图像编辑——API 会生成一张受源图像启发的新图像。
它无法进行像素级精确编辑(例如仅改变汽车颜色而保持其他所有内容不变)。
bash
python3 -c
import json, base64
with open(<源文件路径>, rb) as f:
b64 = base64.b64encode(f.read()).decode()
payload = {
model: grok-imagine-image,
prompt: <编辑提示词>,
image_url: fdata:image/png;base64,{b64},
n: 1,
responseformat: b64json
}
with open(/tmp/imgeditpayload.json, w) as f:
json.dump(payload, f)
print(载荷已就绪)
&& \
curl -s https://api.x.ai/v1/images/generations \
-H Authorization: Bearer $XAIAPIKEY \
-H Content-Type: application/json \
-d @/tmp/imgeditpayload.json | python3 -c
import json, sys, base64, os, time
os.makedirs(os.path.expanduser(~/.openclaw/media), exist_ok=True)
r = json.load(sys.stdin)
imgdata = base64.b64decode(r[data][0][b64json])
fpath = os.path.expanduser(f~/.openclaw/media/edited_{int(time.time())}.png)
with open(fpath, wb) as f:
f.write(img_data)
print(fpath)
bash
curl -s https://api.x.ai/v1/images/generations \
-H Authorization: Bearer $XAIAPIKEY \
-H Content-Type: application/json \
--data {
model: grok-imagine-image,
prompt: <编辑提示词>,
image_url: <公开图像URL>,
n: 1,
responseformat: b64json
} | python3 -c
import json, sys, base64, os, time
os.makedirs(os.path.expanduser(~/.openclaw/media), exist_ok=True)
r = json.load(sys.stdin)
imgdata = base64.b64decode(r[data][0][b64json])
fpath = os.path.expanduser(f~/.openclaw/media/edited_{int(time.time())}.png)
with open(fpath, wb) as f:
f.write(img_data)
print(fpath)
使用带有风格提示词的编辑功能,例如:
消息工具:action=send, channel=telegram, target=
message=<标题>, filePath=~/.openclaw/media/<文件>.png
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 grok-imagine-image-pro-1776420050 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 grok-imagine-image-pro-1776420050 技能
skillhub install grok-imagine-image-pro-1776420050
文件大小: 2.87 KB | 发布时间: 2026-4-17 19:14