用途
generate-image 用于借助 AI 模型生成或编辑图像,覆盖照片、写实图像、艺术插画、概念艺术、演示或文档视觉素材,以及图像编辑与修改等通用图像生成需求。
如果需求是流程图、电路图、生物通路、系统架构图、CONSORT 图等技术或示意图,则应改用 scientific-schematics。
主要能力
该技能通过 OpenRouter 的图像生成模型生成和编辑高质量图像,涉及以下模型:
- google/gemini-3-pro-image-preview:默认模型,高质量,支持生成与编辑。
- black-forest-labs/flux.2-pro:快速、高质量,支持生成与编辑。
- black-forest-labs/flux.2-flex:快速且成本较低,但质量不如 pro,仅支持生成。
选择建议:追求质量可用 gemini-3-pro 或 flux.2-pro;需要编辑可用 gemini-3-pro 或 flux.2-pro;仅生成且关注成本可用 flux.2-flex。
图像以 base64 编码的数据 URL 返回,并自动保存为 PNG 文件。脚本同时支持不同 OpenRouter 模型返回的图像与内容响应格式。生成时间因模型而异,通常为 5 至 30 秒。编辑时输入图像会被编码为 base64 发送给模型,支持的输入格式包括 PNG、JPEG、GIF、WebP。
使用方式
使用 scripts/generate_image.py 脚本生成或编辑图像。
生成新图像:
python scripts/generate_image.py "A beautiful sunset over mountains"
编辑已有图像:
python scripts/generate_image.py "Make the sky purple" --input photo.jpg
生成或编辑后,图像会保存为当前目录下的 generated_image.png。
API 密钥配置
脚本需要 OpenRouter API 密钥。运行前需检查用户是否已配置密钥:在项目目录或父目录中查找 .env 文件,检查其中是否有 OPENROUTER_API_KEY=<key>。如果未找到,需要用户创建 .env 文件并写入 OPENROUTER_API_KEY=your-api-key-here,或设置环境变量 export OPENROUTER_API_KEY=your-api-key-here。API 密钥可从 https://openrouter.ai/keys 获取。脚本会自动检测 .env 文件,并在缺少密钥时给出明确错误提示。
脚本参数
- prompt(必填):要生成图像的文本描述,或编辑指令。
- --input 或 -i:用于编辑的输入图像路径(启用编辑模式)。
- --model 或 -m:OpenRouter 模型 ID(默认:google/gemini-3-pro-image-preview)。
- --output 或 -o:输出文件路径(默认:generated_image.png)。
- --api-key:OpenRouter API 密钥(覆盖 .env 文件)。
常见用法示例
指定模型:
python scripts/generate_image.py "A cat in space" --model "black-forest-labs/flux.2-pro"
自定义输出路径:
python scripts/generate_image.py "Abstract art" --output artwork.png
使用指定模型编辑:
python scripts/generate_image.py "Add sunglasses to the person" --input portrait.png --model "black-forest-labs/flux.2-pro"
编辑并自定义输出:
python scripts/generate_image.py "Remove the text from the image" --input screenshot.png --output cleaned.png
生成多张图像时,可用不同提示词或输出路径多次运行脚本:
python scripts/generate_image.py "Image 1 description" --output image1.png python scripts/generate_image.py "Image 2 description" --output image2.png
适用场景
科学文档:为论文生成概念插图,例如“Microscopic view of cancer cells being attacked by immunotherapy agents, scientific illustration style”,输出到 figures/immunotherapy_concept.png;或为演示生成视觉图,例如“DNA double helix structure with highlighted mutation site, modern scientific visualization”,输出到 slides/dna_mutation.png。
演示与海报:生成标题页背景,例如“Abstract blue and white background with subtle molecular patterns, professional presentation style”,输出到 slides/background.png;或生成海报主视觉,例如“Laboratory setting with modern equipment, photorealistic, well-lit”,输出到 poster/hero.png。
通用视觉内容:生成网站或文档图片,例如“Professional team collaboration around a digital whiteboard, modern office”,输出到 docs/team_collaboration.png;或生成营销素材,例如“Futuristic AI brain concept with glowing neural networks”,输出到 marketing/ai_concept.png。
限制与错误处理
脚本会针对缺少 API 密钥(含配置说明)、API 错误(含状态码)、意外响应格式、缺少依赖(requests 库)等情况给出明确错误信息。若脚本失败,应先阅读错误信息并解决问题后再重试。
图像编辑建议:明确说明要修改的内容(例如“change the sky to sunset colors”而非“edit the sky”);尽量引用图像中的具体元素;使用清晰详细的编辑指令以获得更好效果。Gemini 3 Pro 与 FLUX.2 Pro 均通过 OpenRouter 支持图像编辑。
费用信息可查看 OpenRouter 定价:https://openrouter.ai/models。
与其他技能的配合
- scientific-schematics:用于技术图、流程图、电路图、通路图。
- generate-image:用于照片、插画、艺术作品、视觉概念。
- scientific-slides:可与 generate-image 结合,制作视觉丰富的演示。
- latex-posters:可用 generate-image 生成海报视觉与主视觉图。