# Gemini Image

> 使用 Gemini 生成图片并自动保存到本地。用法：/gemini-image <描述> [--size 1024x1024] [--dir ~/Pictures/gemini-images]

- Skill: `zane98/gemini-image` (Agent Skill)
- Install (CLI): `npx skillmds@latest add zane98/gemini-image`
- Raw SKILL.md: https://api.skillmd.com/api/skills/zane98/gemini-image/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: zane98 (https://skillmd.com/u/zane98)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/zane98/gemini-image

---


# Gemini Image

生成图片并自动保存为本地文件，返回文件路径。

## 参数

- `--size`：图片尺寸，如 `512x512`、`1024x1024`、`4096x4096`，默认 `1024x1024`
- `--dir`：保存目录，默认 `~/Pictures/gemini-images`

## 步骤

1. 从用户输入提取图片描述、`--size`、`--dir` 参数

2. 用 Python 调用 API 并保存图片：

```python
import json, urllib.request, subprocess, base64, re, os
from datetime import datetime
from pathlib import Path

def read_env(var):
    val = os.environ.get(var, "")
    if val:
        return val
    try:
        return subprocess.check_output(
            f"grep 'export {var}' ~/.zshrc ~/.bashrc 2>/dev/null | tail -1 | cut -d'\"' -f2",
            shell=True
        ).decode().strip()
    except:
        return ""

api_key = read_env("GEMINI_API_KEY")
base_url = read_env("GEMINI_BASE_URL") or "https://generativelanguage.googleapis.com/v1beta/openai"

prompt = "<图片描述>"
size = "<尺寸，默认1024x1024>"
save_dir = Path("<保存目录，默认~/Pictures/gemini-images>").expanduser()
save_dir.mkdir(parents=True, exist_ok=True)

payload = {
    "model": "Nano-Banana-2",
    "messages": [{"role": "user", "content": prompt}],
    "size": size
}

req = urllib.request.Request(
    f"{base_url}/chat/completions",
    data=json.dumps(payload).encode(),
    headers={
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
)
resp = urllib.request.urlopen(req, timeout=120)
data = json.loads(resp.read())
content = data["choices"][0]["message"]["content"]

match = re.search(r'data:image/(jpeg|png|gif|webp);base64,([A-Za-z0-9+/=]+)', content)
if match:
    ext = "jpg" if match.group(1) == "jpeg" else match.group(1)
    img_data = base64.b64decode(match.group(2))
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    filepath = save_dir / f"gemini_{timestamp}.{ext}"
    filepath.write_bytes(img_data)
    print(f"Image saved to: {filepath}")
else:
    print(content)
```

3. 输出图片保存路径，供用户用 `open <路径>` 查看

