关于 nano-banana
一项使用 Google Gemini 图像生成模型按用户请求生成图片的技能。它通过 Python 和 `google-genai` 调用模型,并从 `.env` 读取 API 密钥。
主要功能
- 01使用 Gemini 生成图片
- 02根据用户请求构建提示词
- 03通过 Python 使用 `google-genai`
- 04从 `.env` 读取 `GEMINI_API_KEY`
适用场景
- 01根据文本请求创建图片
- 02使用用户所用语言的提示词生成图片
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ aturilin/ nano-banana --skill nano-banana安装后在 Claude Code 中输入/nano-banana确认已加载。
附带文件README.mdskills/nano-banana/nano_banana.py
详细介绍
Nano Banana — 图像生成器
使用 Google Gemini 的图像生成模型生成图像。
用户请求: $ARGUMENTS
前置条件
- Python 3.10+
google-genai软件包(pip install google-genai).env文件中的GEMINI_API_KEY(在 aistudio.google.com 获取免费密钥)
生成方法
编写并运行此 Python 脚本:
import os
import sys
from pathlib import Path
from datetime import datetime
# Load .env
env_file = Path(".env")
if env_file.exists():
for line in env_file.read_text().splitlines():
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
os.environ[key] = value
from google import genai
from google.genai import types
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
print("❌ GEMINI_API_KEY not found. Add it to .env file.")
sys.exit(1)
client = genai.Client(api_key=api_key)
prompt = "<USER'S IMAGE DESCRIPTION HERE>"
try:
response = client.models.generate_content(
model="gemini-3.1-flash-image-preview",
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=['IMAGE', 'TEXT']
)
)
if not response.candidates:
print("❌ No response — image may have been blocked by safety filter")
sys.exit(1)
candidate = response.candidates[0]
if not candidate.content or not candidate.content.parts:
print("❌ No content in response — try a different prompt")
sys.exit(1)
for part in candidate.content.parts:
if part.inline_data is not None:
ext = "png" if "png" in part.inline_data.mime_type else "jpg"
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filepath = f"images/{timestamp}.{ext}"
Path("images").mkdir(exist_ok=True)
Path(filepath).write_bytes(part.inline_data.data)
print(f"✅ Image saved: {filepath}")
break
else:
print("❌ No image in response — model returned text only")
sys.exit(1)
except Exception as e:
print(f"❌ Error: {e}")
sys.exit(1)
重要事项
- 根据用户的请求,将
<USER'S IMAGE DESCRIPTION HERE>替换为实际提示词 - 提示词必须使用与用户相同的语言
- 如果未安装
google-genai,请先安装:pip install google-genai - 生成图像后,使用 Read 工具向用户展示图像
评论
支持 Markdown