Banana API - Nano Banana Image Generation
Streamlined client for Nano Banana's Gemini-3-Pro-Image-Preview API. Automatically handles the annoying base64 workflow, image compression, and Discord sending.
What This Skill Solves
The Problem with Gemini API:
- - Input/output is only base64 (no URLs)
- Large images = massive base64 strings (2MB+)
- Manual encoding/decoding is painful
- No built-in Discord integration
This Skill Provides:
- - ✅ Automatic image compression (512px default)
- ✅ Transparent base64 encoding/decoding
- ✅ Smart filename generation
- ✅ Auto-send to Discord
- ✅ Both text-to-image and image-to-image editing
Quick Start
Text-to-Image
CODEBLOCK0
Image Editing
CODEBLOCK1
With Discord Auto-Send
CODEBLOCK2
Configuration
API Key (Choose One)
Option 1: Config File (Recommended - Persistent)
CODEBLOCK3
Option 2: Environment Variable
CODEBLOCK4
Option 3: Command Line (One-time)
CODEBLOCK5
Output Location
Generated images are saved to:
CODEBLOCK6
View Current Config
CODEBLOCK7
Usage Examples
Basic Generation
CODEBLOCK8
Image Editing (Inpainting/Restyle)
CODEBLOCK9
Discord Integration
CODEBLOCK10
Advanced Options
CODEBLOCK11
Best Practices
Image Editing Tips
- 1. Compress input automatically: Script resizes to 512px by default
- Be specific: "wearing black leather jacket" > "cool outfit"
- Preserve identity: Face features are usually maintained well
- Aspect ratio: Add to prompt, but actual output depends on model
Prompt Engineering
- - Style keywords: "oil painting", "anime style", "photorealistic"
- Lighting: "dramatic lighting", "soft golden hour", "neon lights"
- Quality: "high quality", "detailed", "8k"
File Naming
The script auto-generates filenames:
- - Format: INLINECODE0
- Description is cleaned from prompt (first 30 chars)
- Use
--name for custom suffix: INLINECODE2
Technical Details
Image Processing Pipeline
CODEBLOCK12
API Flow
CODEBLOCK13
Error Handling
- - Input file not found → Clear error message
- API key missing → Prompt to set BANANAAPIKEY
- Image too large → Automatic compression
- API error → JSON error details printed
- Discord send fail → Warning but continues
Limitations
- - Output always base64: Cannot be changed (Gemini limitation)
- No URL input: Must download images locally first
- Single image output: Gemini returns one image per request
- Response time: 10-60 seconds depending on complexity
When to Use This vs Other Tools
| Use Case | Recommended Tool |
|---|
| Quick Gemini image | banana-api ✅ |
| ComfyUI workflows |
comfyui-gen |
| DALL-E / OpenAI | Use their direct API |
| Stable Diffusion | comfyui-gen |
Troubleshooting
| Problem | Solution |
|---|
| "PIL not available" | Install: INLINECODE3 |
| "API key required" |
Run
--setup to save key, or set
BANANA_API_KEY env var |
| "No image data found" | Model may have returned text only; try different prompt |
| Large output files | Normal for high-res images (500KB-2MB) |
| Discord send fails | Check channel ID and openclaw CLI access |
Configuration Priority
API Key is loaded in this order (first found wins):
- 1.
--api-key command line argument - INLINECODE7 environment variable
- INLINECODE8 config file
- ❌ Error if none found
To persist the API key for future use:
CODEBLOCK14
Script Reference
CODEBLOCK15
Integration with Workflows
As Part of a Larger Pipeline
CODEBLOCK16
From Another Skill
Call the script directly:
CODEBLOCK17
Banana API - Nano Banana 图像生成
Nano Banana Gemini-3-Pro-Image-Preview API 的精简客户端。自动处理繁琐的 base64 工作流程、图像压缩和 Discord 发送。
本技能解决的问题
Gemini API 的问题:
- - 输入/输出仅支持 base64(不支持 URL)
- 大图像 = 庞大的 base64 字符串(2MB+)
- 手动编码/解码非常痛苦
- 没有内置的 Discord 集成
本技能提供的功能:
- - ✅ 自动图像压缩(默认 512px)
- ✅ 透明的 base64 编码/解码
- ✅ 智能文件名生成
- ✅ 自动发送到 Discord
- ✅ 支持文生图和图生图编辑
快速开始
文生图
bash
python3 scripts/banana_gen.py 窗台上的一只可爱毛茸茸的猫
图像编辑
bash
python3 scripts/banana_gen.py 变成摇滚演唱会场景,穿着皮夹克 \
--image /path/to/photo.png
带 Discord 自动发送
bash
python3 scripts/banana_gen.py 夜晚的赛博朋克城市景观 \
--channel-id 1478746465328435412
配置
API 密钥(选择一种)
选项 1:配置文件(推荐 - 持久化)
bash
交互式设置(存储在 ~/.openclaw/workspace/config/banana-api.json)
python3 scripts/banana_gen.py --setup
或手动创建配置文件
echo {api_key: sk-your-key-here} > ~/.openclaw/workspace/config/banana-api.json
选项 2:环境变量
bash
export BANANAAPIKEY=sk-xxxxxxxx
选项 3:命令行(一次性)
bash
python3 scripts/banana_gen.py 提示词 --api-key sk-xxx
输出位置
生成的图像保存到:
~/.openclaw/workspace/photos/{描述}-{时间戳}.png
查看当前配置
bash
python3 scripts/banana_gen.py --show-config
使用示例
基础生成
bash
简单提示词
python3 scripts/banana_gen.py 山上的日落
带宽高比提示
python3 scripts/banana_gen.py 战士的肖像 --ratio 2:3
自定义文件名标签
python3 scripts/banana_gen.py 弹钢琴的猫 --name cat-piano
图像编辑(修复/重绘)
bash
更改场景/服装
python3 scripts/banana_gen.py 在海滩上穿着红色连衣裙 \
--image ~/photos/portrait.png
更改背景
python3 scripts/banana_gen.py 站在夜晚的东京天际线前 \
--image ~/photos/selfie.png \
--name tokyo-night
艺术风格转换
python3 scripts/banana_gen.py 油画风格,文艺复兴肖像 \
--image ~/photos/photo.jpg
Discord 集成
bash
自动发送到频道
python3 scripts/banana_gen.py 喷火的龙 \
--channel-id 1478746465328435412
自动发送并自定义名称
python3 scripts/banana_gen.py 可爱的蓝发动漫女孩 \
--channel-id 1478746465328435412 \
--name anime-girl
仅本地保存(不发送到 Discord)
python3 scripts/banana_gen.py 提示词 --no-send
高级选项
bash
自定义输出路径
python3 scripts/banana_gen.py 提示词 --output ~/Desktop/my-image.png
不同模型(如果可用)
python3 scripts/banana_gen.py 提示词 --model gemini-2.5-flash-image
完整示例
python3 scripts/banana_gen.py 在古老图书馆施法的巫师 \
--image ~/photos/me.png \
--ratio 2:3 \
--name wizard-me \
--channel-id 1478746465328435412
最佳实践
图像编辑技巧
- 1. 自动压缩输入:脚本默认调整为 512px
- 具体描述:穿着黑色皮夹克 > 酷炫服装
- 保持身份特征:面部特征通常能很好地保留
- 宽高比:添加到提示词中,但实际输出取决于模型
提示词工程
- - 风格关键词:油画、动漫风格、照片级真实
- 光照:戏剧性光照、柔和黄金时刻、霓虹灯
- 质量:高质量、细节丰富、8k
文件命名
脚本自动生成文件名:
- - 格式:banana-{描述}-{时间戳}.png
- 描述从提示词中提取(前 30 个字符)
- 使用 --name 自定义后缀:banana-{名称}-{时间戳}.png
技术细节
图像处理流程
输入图像 → 调整至 512px → JPEG 压缩(85%)→ Base64 编码 → API
↓
输出 ← Base64 解码 ← PNG 保存 ← 响应
API 流程
- 1. 压缩输入图像(如果提供)
- 构建 Gemini API 请求,包含 base64 inlineData
- POST 到 /v1beta/models/{model}:generateContent
- 从 response.candidates[0].content.parts 提取图像
- 解码 base64 并保存到 workspace/photos/
- 发送到 Discord(如果提供了 channel-id)
错误处理
- - 输入文件未找到 → 清晰的错误信息
- API 密钥缺失 → 提示设置 BANANAAPIKEY
- 图像过大 → 自动压缩
- API 错误 → 打印 JSON 错误详情
- Discord 发送失败 → 发出警告但继续执行
限制
- - 输出始终为 base64:无法更改(Gemini 限制)
- 不支持 URL 输入:必须先将图像下载到本地
- 单图像输出:Gemini 每次请求返回一张图像
- 响应时间:10-60 秒,取决于复杂度
何时使用本工具 vs 其他工具
| 使用场景 | 推荐工具 |
|---|
| 快速 Gemini 图像生成 | banana-api ✅ |
| ComfyUI 工作流 |
comfyui-gen |
| DALL-E / OpenAI | 使用其直接 API |
| Stable Diffusion | comfyui-gen |
故障排除
| 问题 | 解决方案 |
|---|
| PIL not available | 安装:pip install Pillow |
| API key required |
运行 --setup 保存密钥,或设置 BANANA
APIKEY 环境变量 |
| No image data found | 模型可能只返回了文本;尝试不同的提示词 |
| 输出文件过大 | 高分辨率图像正常(500KB-2MB) |
| Discord 发送失败 | 检查频道 ID 和 openclaw CLI 访问权限 |
配置优先级
API 密钥按以下顺序加载(先找到的优先):
- 1. --api-key 命令行参数
- BANANAAPIKEY 环境变量
- ~/.openclaw/workspace/config/banana-api.json 配置文件
- ❌ 如果都未找到则报错
要持久化保存 API 密钥供以后使用:
bash
python3 scripts/banana_gen.py --setup
脚本参考
bash
python3 scripts/banana_gen.py [提示词] [选项]
选项:
--image, -i PATH 用于编辑的输入图像
--ratio, -r RATIO 宽高比提示(例如 2:3, 16:9)
--model, -m MODEL 模型名称(默认:gemini-3-pro-image-preview)
--output, -o PATH 自定义输出路径
--name, -n NAME 文件名后缀/标签
--channel-id, -c ID 用于自动发送的 Discord 频道 ID
--no-send 跳过 Discord 发送
--api-key, -k KEY API 密钥(或设置 BANANAAPIKEY)
--setup 交互式设置以保存 API 密钥
--show-config 显示当前配置
工作流集成
作为更大流程的一部分
bash
生成 + 发送 + 引用
IMAGE=$(python3 scripts/banana
gen.py 可爱的猫 --name kitty | grep OUTPUTPATH: | cut -d: -f2-)
echo 已生成:$IMAGE
从其他技能调用
直接调用脚本:
python
import subprocess