OpenRouter Image Generation & Editing
Generate new images or edit existing ones using OpenRouter image-capable models via the Chat Completions API.
Usage
Run the script using absolute path (do NOT cd to the skill directory first):
Generate new image:
CODEBLOCK0
Edit existing image (image-to-image):
CODEBLOCK1
Important: Default OpenClaw delivery path is ~/.openclaw/media/outbound/. Save generated images there so other OpenClaw flows can pick them up easily.
API Key
The script checks for API key in this order:
- 1.
--api-key argument - INLINECODE2 environment variable
Optional OpenRouter attribution headers:
- -
--site-url or INLINECODE4 - INLINECODE5 or INLINECODE6
Model + Image Config
- -
--model <openrouter-model-id> is required (no script default) - Example model: INLINECODE8
- Use
--aspect-ratio for image_config.aspect_ratio (for example 1:1, 16:9) - Use
--image-size for image_config.image_size (1K, 2K, 4K) - Use
--image-config-json '{"key":"value"}' for advanced/provider-specific extras (merged into image_config)
Note: OpenRouter docs show aspect_ratio and image_size as the common image config fields for image generation. Additional keys may exist for specific providers/models (for example Sourceful features). If a request fails, remove unsupported options or switch models.
Note: The script always sends modalities: ["image", "text"]. Image-only models (some FLUX variants) may reject this — if you get an unexpected error with a non-Gemini model, this may be the cause. No workaround is currently exposed via CLI args.
Default Workflow (draft -> iterate -> final)
Goal: iterate quickly before spending time on higher-quality settings.
- - Draft: smaller size / faster model
-
--image-size 1K
- - Iterate: adjust prompt in small diffs and keep a new filename each run
- Final: larger size or higher quality if the selected model supports it
- Example: INLINECODE24
Preflight + Common Failures
-
command -v uv
-
test -n "$OPENROUTER_API_KEY" (or pass
--api-key)
-
test -d ~/.openclaw/media/outbound || mkdir -p ~/.openclaw/media/outbound
- If editing: INLINECODE29
-
Error: No API key provided. -> set
OPENROUTER_API_KEY or pass
--api-key
-
Error loading input image: -> bad path or unreadable file
-
HTTP 400 with model/image config error -> unsupported model or invalid
image_config.aspect_ratio /
image_config.image_size
-
HTTP 401/403 -> invalid key, no model access, or quota/credits issue
-
No image found in response -> model may not support image output or request format rejected
Filename Generation
Generate filenames with the pattern: INLINECODE39
Examples:
- - INLINECODE40
- INLINECODE41
Prompt Handling
- - For generation: pass the user's description as-is unless it is too vague to be actionable.
- For editing: make the requested change explicit and preserve everything else.
Prompt template for precise edits:
Output
- - Save the first returned image to
~/.openclaw/media/outbound/output-name.png by default (pass that full path in --filename) - Supports OpenRouter's base64 data URL image responses (
message.images[0].image_url.url) - Prints the saved file path
- Do not read the image back unless the user asks
Examples
Generate new image:
CODEBLOCK2
Edit existing image:
CODEBLOCK3
Reference
- - OpenRouter docs: https://openrouter.ai/docs/guides/overview/multimodal/image-generation
OpenRouter 图像生成与编辑
使用支持图像功能的 OpenRouter 模型,通过聊天补全 API 生成新图像或编辑现有图像。
使用方法
使用绝对路径运行脚本(请勿先切换到技能目录):
生成新图像:
bash
首先确保出站目录存在
mkdir -p ~/.openclaw/media/outbound
uv run ~/.openclaw/workspace/skills/openrouter-image-generation/scripts/generate_image.py \
--prompt 你的图像描述 \
--filename ~/.openclaw/media/outbound/输出名称.png \
--model google/gemini-2.5-flash-image \
[--aspect-ratio 16:9] \
[--image-size 2K]
编辑现有图像(图像到图像):
bash
首先确保出站目录存在
mkdir -p ~/.openclaw/media/outbound
uv run ~/.openclaw/workspace/skills/openrouter-image-generation/scripts/generate_image.py \
--prompt 编辑指令 \
--filename ~/.openclaw/media/outbound/输出名称.png \
--input-image 输入图像路径.png \
--model google/gemini-2.5-flash-image
重要提示: 默认的 OpenClaw 交付路径为 ~/.openclaw/media/outbound/。将生成的图像保存到该目录,以便其他 OpenClaw 流程可以轻松获取。
API 密钥
脚本按以下顺序检查 API 密钥:
- 1. --api-key 参数
- OPENROUTERAPIKEY 环境变量
可选的 OpenRouter 归属标头:
- - --site-url 或 OPENROUTERSITEURL
- --app-name 或 OPENROUTERAPPNAME
模型 + 图像配置
- - --model 为必填项(脚本无默认值)
- 示例模型:google/gemini-2.5-flash-image
- 使用 --aspect-ratio 设置 imageconfig.aspectratio(例如 1:1、16:9)
- 使用 --image-size 设置 imageconfig.imagesize(1K、2K、4K)
- 使用 --image-config-json {key:value} 设置高级/提供商特定的额外参数(合并到 image_config 中)
注意:OpenRouter 文档将 aspectratio 和 imagesize 列为图像生成的通用图像配置字段。特定提供商/模型可能存在其他键(例如 Sourceful 功能)。如果请求失败,请移除不支持的选项或切换模型。
注意:脚本始终发送 modalities: [image, text]。仅支持图像的模型(某些 FLUX 变体)可能会拒绝此请求——如果使用非 Gemini 模型时遇到意外错误,这可能是原因。目前 CLI 参数未提供解决方案。
默认工作流程(草稿 -> 迭代 -> 最终)
目标:在投入高质量设置之前快速迭代。
- --image-size 1K
- - 迭代:以小幅差异调整提示,每次运行使用新文件名
- 最终:如果所选模型支持,使用更大尺寸或更高质量
- 示例:--image-size 4K --aspect-ratio 16:9
预检 + 常见失败
- command -v uv
- test -n $OPENROUTER
APIKEY(或传递 --api-key)
- test -d ~/.openclaw/media/outbound || mkdir -p ~/.openclaw/media/outbound
- 如果编辑:test -f 输入图像路径.png
- 错误:未提供 API 密钥。 -> 设置 OPENROUTER
APIKEY 或传递 --api-key
- 加载输入图像时出错: -> 路径错误或文件不可读
- HTTP 400 并显示模型/图像配置错误 -> 不支持的模型或无效的 image
config.aspectratio / image
config.imagesize
- HTTP 401/403 -> 密钥无效、无模型访问权限或配额/积分问题
- 响应中未找到图像 -> 模型可能不支持图像输出或请求格式被拒绝
文件名生成
使用以下模式生成文件名:~/.openclaw/media/outbound/yyyy-mm-dd-hh-mm-ss-名称.png
示例:
- - ~/.openclaw/media/outbound/2026-02-26-14-23-05-产品照片.png
- ~/.openclaw/media/outbound/2026-02-26-14-25-30-天空编辑.png
提示处理
- - 生成:直接传递用户的描述,除非描述过于模糊无法执行。
- 编辑:明确说明要进行的更改,并保留其他所有内容。
精确编辑的提示模板:
- - 仅更改:<更改内容>。保持相同:主体、构图/裁剪、姿势、光照、调色板、背景、文字和整体风格。不要添加新对象。
输出
- - 默认将返回的第一张图像保存到 ~/.openclaw/media/outbound/输出名称.png(在 --filename 中传递完整路径)
- 支持 OpenRouter 的 base64 数据 URL 图像响应(message.images[0].image_url.url)
- 打印保存的文件路径
- 除非用户要求,否则不要读取图像
示例
生成新图像:
bash
mkdir -p ~/.openclaw/media/outbound
uv run ~/.openclaw/workspace/skills/openrouter-image-generation/scripts/generate_image.py \
--prompt 一张电影级产品照片:哑光黑色机械键盘放在木质书桌上,温暖的窗光 \
--filename ~/.openclaw/media/outbound/2026-02-26-14-23-05-键盘产品照片.png \
--model google/gemini-2.5-flash-image \
--aspect-ratio 16:9 \
--image-size 2K
编辑现有图像:
bash
mkdir -p ~/.openclaw/media/outbound
uv run ~/.openclaw/workspace/skills/openrouter-image-generation/scripts/generate_image.py \
--prompt 仅更改:将天空改为戏剧性的橙色日落云彩。保持相同:主体、构图、前景光照和整体风格。 \
--filename ~/.openclaw/media/outbound/2026-02-26-14-25-30-日落天空编辑.png \
--model google/gemini-2.5-flash-image \
--input-image 原始照片.jpg
参考
- - OpenRouter 文档:https://openrouter.ai/docs/guides/overview/multimodal/image-generation