Describe
483 字
1 分钟
2026年05月04日 | MiWapAI 图片生成工作流(OpenClaw 版)
MiWapAI 图片生成工作流(OpenClaw 版)
核心信息
| 项目 | 值 |
|---|---|
| API 端点 | https://ai.miwap.com/v1/responses |
| 模型 | gpt-5.5(带 image_generation 工具) |
| 必须直连 | 禁止代理,否则流被截断返回 ~3390 字节 |
关键坑点(必看)
1. curl 响应被截断问题
问题描述:直接用 subprocess.run(['curl', ...]) 调用 curl,响应被截断到 ~3390 字节,无法获取图片。
原因:stdout 管道缓冲问题。
解决方案:必须用以下方式之一:
方案 A:写临时脚本文件再执行(推荐)
script_content = f'''#!/bin/bashcurl -s -N --max-time 180 "https://ai.miwap.com/v1/responses" \\ -H "Authorization: Bearer {API_KEY}" \\ -H "Content-Type: application/json" \\ -d '{json.dumps(payload)}' \\ 2>&1'''
script_file = '/tmp/miwap_gen.sh'with open(script_file, 'w') as f: f.write(script_content)os.chmod(script_file, 0o755)
result = subprocess.run(['/bin/bash', script_file], capture_output=True, timeout=195)resp = result.stdout.decode('utf-8', errors='replace')os.unlink(script_file)方案 B:用 bash -l -c
cmd = 'curl -s -N --max-time 180 "https://ai.miwap.com/v1/responses" ...'result = subprocess.run(['bash', '-l', '-c', cmd], capture_output=True, timeout=195)2. payload 过长导致 “Argument list too long”
问题描述:prompt 加上 base64 图片后,整个 curl 命令行超过系统限制。
解决方案:将 payload 写到文件,用 @payload_file 方式传递:
payload_file = '/tmp/miwap_payload.json'with open(payload_file, 'w') as f: json.dump(payload, f)
script_content = f'''#!/bin/bashcurl -s -N --max-time 180 "https://ai.miwap.com/v1/responses" \\ -H "Authorization: Bearer {API_KEY}" \\ -H "Content-Type: application/json" \\ -d @{payload_file} \\ 2>&1'''3. 必须加 partial_images: 1
不加 partial_images 参数会导致流不稳定,图片可能丢失。
完整请求格式
import os, json, base64, subprocess, re
API_KEY = os.environ.get('MIWAPAI_API_KEY') # 从环境变量读取TIMEOUT = 300
payload = { 'model': 'gpt-5.5', 'stream': True, 'instructions': 'You are an artist.', 'input': [{ 'type': 'message', 'role': 'user', 'content': [ {'type': 'input_image', 'image_url': f'data:image/png;base64,{图片base64}'}, {'type': 'input_text', 'text': '你的prompt'} ] }], 'tools': [{'type': 'image_generation', 'partial_images': 1}]}完整 Python 脚本
#!/usr/bin/env python3"""OpenClaw miwapai 图片生成工作流"""
import osimport sysimport jsonimport base64import reimport subprocessfrom pathlib import Path
# 从环境变量读取 API KeyAPI_KEY = os.environ.get('MIWAPAI_API_KEY', '')API_ENDPOINT = 'https://ai.miwap.com/v1/responses'MODEL = 'gpt-5.5'TIMEOUT = 300
def generate_image(prompt: str, output_path: str = None, ref_image_path: str = None) -> dict: """ 调用 miwapai 生成图片
Args: prompt: 图片描述 output_path: 输出路径,默认写入 downloads 目录 ref_image_path: 参考图片路径(用于图生图)
Returns: dict: {'success': bool, 'path': str, 'error': str} """ if not API_KEY: return {'success': False, 'path': '', 'error': 'MIWAPAI_API_KEY not set'}
if output_path is None: downloads = Path.home() / '.openclaw' / 'media' / 'qqbot' / 'downloads' output_path = str(downloads / 'miwap_output.png')
# 构建 content content = [] if ref_image_path: with open(ref_image_path, 'rb') as f: ref_b64 = base64.b64encode(f.read()).decode() content.append({'type': 'input_image', 'image_url': f'data:image/png;base64,{ref_b64}'}) content.append({'type': 'input_text', 'text': prompt})
payload = { 'model': MODEL, 'stream': True, 'instructions': 'You are an artist.', 'input': [{'type': 'message', 'role': 'user', 'content': content}], 'tools': [{'type': 'image_generation', 'partial_images': 1}] }
# 写到文件再curl(避免命令行过长) payload_file = '/tmp/miwap_payload.json' with open(payload_file, 'w') as f: json.dump(payload, f)
script_content = f'''#!/bin/bashcurl -s -N --max-time {TIMEOUT} "{API_ENDPOINT}" \\ -H "Authorization: Bearer {API_KEY}" \\ -H "Content-Type: application/json" \\ -d @{payload_file} \\ 2>&1'''
script_file = '/tmp/miwap_gen.sh' with open(script_file, 'w') as f: f.write(script_content) os.chmod(script_file, 0o755)
try: result = subprocess.run(['/bin/bash', script_file], capture_output=True, timeout=TIMEOUT + 15) resp = result.stdout.decode('utf-8', errors='replace') except subprocess.TimeoutExpired: os.unlink(script_file) os.unlink(payload_file) return {'success': False, 'path': '', 'error': 'timeout'} finally: if os.path.exists(script_file): os.unlink(script_file) if os.path.exists(payload_file): os.unlink(payload_file)
if not resp or 'partial_image_b64' not in resp: return {'success': False, 'path': '', 'error': f'No image. resp_len={len(resp)}'}
matches = re.findall(r'"partial_image_b64":"([^"]+)"', resp) if not matches: return {'success': False, 'path': '', 'error': 'b64 not found'}
full_b64 = matches[-1] img_data = base64.b64decode(full_b64)
# 检查是否 SVG if img_data[:5] == b'<?xml' or img_data[:4] == b'<svg': return {'success': False, 'path': '', 'error': 'Got SVG'}
os.makedirs(os.path.dirname(output_path), exist_ok=True) with open(output_path, 'wb') as f: f.write(img_data)
return {'success': True, 'path': output_path, 'error': ''}
if __name__ == '__main__': prompt = sys.argv[1] output_path = sys.argv[2] if len(sys.argv) > 2 else None ref_path = sys.argv[3] if len(sys.argv) > 3 else None
result = generate_image(prompt, output_path, ref_path)
if result['success']: print(f"OK: {result['path']}") print(f"Size: {os.path.getsize(result['path'])} bytes") else: print(f"FAIL: {result['error']}") sys.exit(1)使用方式
纯文字生图
python3 miwap_image.py "a beautiful sunset"指定输出路径
python3 miwap_image.py "a beautiful sunset" /path/to/output.png参考图生图(图生图)
python3 miwap_image.py "change to blue color" /path/to/output.png /path/to/reference.pngPython 调用
from miwap_image import generate_image
# 纯文字生图result = generate_image("a beautiful cat")
# 带参考图result = generate_image("make it blue", ref_image_path="/path/to/ref.png")提示词稳定性规律
| 类型 | 示例 | 成功率 |
|---|---|---|
| 短单词 | blue circle, woman | 极高 |
| 中等长度 | fluffy orange tabby cat | 高 |
| 长描述 | 50+ 词含详细细节 | 不稳定,易超时 |
| 含中文 | 古风美女 | 不稳定,容易失败 |
建议:
- 短 prompt 直接成功
- 长 prompt 拆成多段测试
- 中文 prompt 建议转英文
已知问题
- 代理不能用:必须直连,用代理 100% 截断
- 长 prompt 超时:超过 180 秒可能截断,建议 TIMEOUT=300
- 偶尔出 SVG:如果图片前几个字节是
<?xml或<svg,说明返回了 SVG 而非 PNG - 响应被截断:必须用脚本文件方式调用 curl,不能直接 subprocess.run curl 命令
OpenClaw 集成
脚本路径:~/.openclaw/workspace/files/代码/miwap_image.py
环境变量:MIWAPAI_API_KEY(需要添加到 OpenClaw 配置或系统环境变量)
在 OpenClaw 里调用时:
result = subprocess.run( ['python3', '/path/to/miwap_image.py', prompt, output_path], capture_output=True, timeout=320)发送图片到 QQ:
openclaw message send --channel qqbot --media /path/to/image.png --target <QQ号>最后更新:2026-05-04
分享
如果这篇文章对你有帮助,欢迎分享给更多人!
2026年05月04日 | MiWapAI 图片生成工作流(OpenClaw 版)
https://www.yunio.cn/posts/2026-05-04-miwapai生图教程/ 部分信息可能已经过时
相关文章 智能推荐
1
2026年05月05日 | 我把 MiWapAI 生图链路从抽风修到能用,顺手画了一堆图
技术教程 技术复盘:从 sharp 缺失、流式断流、QQ 压图,到 miwap_image.py 稳定出图,顺手把三花猫、汉服写真和广告海报都跑出来了。
2
2026年05月02日 | OpenClaw 对接 NapCat 新版教程(异机部署)
技术教程 技术教程:OpenClaw 与 NapCat 不在同一台机器时,如何正确配置 HTTP API、反向 WebSocket、插件和白名单
3
2026-04-11 | 自拍场景智能生成方案
技术教程 让媚娘的自拍场景根据对话内容动态生成,每天不重复
4
2026年04月28日 | Docker常用命令速查表
技术教程 技术教程:Docker 常用命令按镜像、容器、日志、网络、清理几类整理,适合日常开发快速查阅
5
2026年04月24日 | Docker常用命令速查表
技术教程 技术教程:Docker常用命令速查表






