AI图像生成与编辑服务

基于Google Gemini 2.5 Flash Image模型的AI驱动图像生成与编辑服务,支持文本生成图像、图像编辑、多图像合成等功能,适用于创意设计、电子商务和内容创作等场景。

  • by lpenguin
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 lpenguin/nanobanana-mcp

使用说明

纳米香蕉mcp

一个模型上下文协议(MCP)服务器,用于使用谷歌的Gemini 2.5 Flash Image模型(绰号“nanobanana”)创建和编辑图像。该服务器采用TypeScript构建,通过简单的MCP接口提供强大的AI驱动的图像生成和操作功能。

特性

  • 文本到图像生成:根据详细的文本描述创建高质量的图像
  • 图像编辑:使用自然语言提示修改现有图像
  • 多图像合成:将多幅图像组合成创意作品
  • 灵活的纵横比:支持各种宽高比(1:1、16:9、9:16等)
  • 由Google Gemini提供技术支持:使用最先进的Gemini 2.5 Flash Image型号

先决条件

安装

npm install -g @lpenguin/nanobanana-mcp

或者在项目中本地安装:

npm install @lpenguin/nanobanana-mcp

配置

将您的Google Gemini API密钥设置为环境变量:

export GEMINI_API_KEY="your-api-key-here"

用法

作为MCP服务器

添加到MCP客户端配置中:

{
  "mcpServers": {
    "nanobanana": {
      "command": "npx",
      "args": ["@lpenguin/nanobanana-mcp"],
      "env": {
        "GEMINI_API_KEY": "your-api-key-here"
      }
    }
  }
}

或者,如果全局安装:

{
  "mcpServers": {
    "nanobanana": {
      "command": "nanobanana-mcp",
      "env": {
        "GEMINI_API_KEY": "your-api-key-here"
      }
    }
  }
}

可用工具

generate_image

使用谷歌的Gemini 2.5 Flash image模型从文本提示中生成新图像。

参数:

  • prompt (字符串,必填):要生成的图像的详细文本描述。具体说明风格、构图、灯光、颜色和情绪。
  • outputPath (字符串,必填):保存生成图像的路径(PNG格式)
  • aspectRatio (字符串,可选):图像的纵横比-“1:1”、“2:3”、“3:2”、“3:4”、”4:3“、”4:5“、”5:4“、”9:16“、”16:9“、”21:9“(默认值:”1:1“)

例子:

{
  "prompt": "A photorealistic close-up portrait of an elderly Japanese ceramicist with deep, sun-etched wrinkles and a warm, knowing smile. He is carefully inspecting a freshly glazed tea bowl. The setting is his rustic, sun-drenched workshop. The scene is illuminated by soft, golden hour light streaming through a window.",
  "outputPath": "./images/ceramicist.png",
  "aspectRatio": "4:3"
}

edit_image

使用文本提示编辑现有图像。添加、删除或修改元素,同时保留原始样式和构图。

参数:

  • inputPath (string,必填):输入图像文件的路径
  • prompt (string,必填):详细描述要更改、添加或删除的内容。明确保留不变的元素。
  • outputPath (字符串,必填):保存编辑图像的路径(PNG格式)
  • aspectRatio (字符串,可选):输出图像的纵横比(默认值:匹配输入)

例子:

{
  "inputPath": "./images/cat.png",
  "prompt": "Add a small, knitted wizard hat on the cat's head. Make it look like it's sitting comfortably and matches the soft lighting of the photo.",
  "outputPath": "./images/cat_with_hat.png"
}

合成图像

使用文本提示将多幅图像组合成一幅作品。非常适合产品模型、风格转换和创意拼贴。

参数:

  • imagePaths (字符串数组,必填):输入图像的路径数组(建议最多3个图像)
  • prompt (string,必填):详细描述如何组合图像。按顺序(第一、第二、第三)参考图像。
  • outputPath (字符串,必填):保存合成图像的路径(PNG格式)
  • aspectRatio (字符串,可选):输出图像的纵横比(默认值:“1:1”)

例子:

{
  "imagePaths": ["./images/dress.png", "./images/model.png"],
  "prompt": "Create a professional e-commerce fashion photo. Take the blue floral dress from the first image and let the woman from the second image wear it. Generate a realistic, full-body shot.",
  "outputPath": "./images/fashion_shot.png",
  "aspectRatio": "2:3"
}

提示提示

为了在生成或编辑图像时获得最佳效果:

  1. 具有描述性:使用详细的叙述性描述,而不是关键字列表
  2. 指定样式:提及艺术风格、摄影术语、灯光和情绪
  3. 使用摄影术语:对于逼真的图像,请提及相机角度、镜头类型和照明
  4. 保留详细信息:编辑时,明确说明哪些内容应保持不变
  5. 参考顺序:合成时,将图像称为“第一个”、“第二个”和“第三个”

示例提示

照片级真实感:

A photorealistic close-up portrait of an elderly Japanese ceramicist with deep, 
sun-etched wrinkles and a warm, knowing smile. Captured with an 85mm portrait lens.

风格化:

A kawaii-style sticker of a happy red panda wearing a tiny bamboo hat. Bold, clean 
outlines, simple cel-shading, vibrant color palette. White background.

标志/文字:

Create a modern, minimalist logo for a coffee shop called 'The Daily Grind'. 
Clean, bold, sans-serif font. Simple coffee bean icon. Black and white.

发展

设置

# Clone the repository
git clone https://github.com/lpenguin/nanobanana-mcp.git
cd nanobanana-mcp

# Install dependencies
npm install

# Build
npm run build

# Run integration tests (requires GEMINI_API_KEY)
npm test

# Run linter
npm run lint

# Fix linting issues
npm run lint:fix

# Watch mode for development
npm run watch

测试

该项目包括验证MCP服务器正常工作的集成测试:

# Set your API key
export GEMINI_API_KEY="your-api-key-here"

# Run the integration test
npm test

集成测试:

  • 验证服务器能否成功启动
  • 测试MCP协议通信
  • 列出所有可用工具
  • 不执行实际的映像操作(没有API调用)

GitHub Actions工作流还对每个推送和拉取请求运行这些测试。

项目结构

nanobanana-mcp/
├── src/
│   └── index.ts              # Main server implementation
├── dist/                     # Compiled output (generated)
├── .github/
│   └── workflows/
│       ├── publish.yml       # CI/CD workflow
│       └── integration-test.yml  # Integration test workflow
├── package.json              # Package configuration
├── tsconfig.json             # TypeScript configuration
├── .eslintrc.json            # ESLint configuration
├── .gitignore                # Git ignore rules
├── test-integration.js       # Integration test script
└── README.md                 # This file

关于Gemini 2.5 Flash图像(Nanobanana)

此MCP服务器使用谷歌的Gemini 2.5 Flash Image模型,被开发人员社区昵称为“nanobanana”。主要特点:

  • 文本到图像:根据描述生成图像
  • 图像编辑:通过对话添加、删除或修改元素
  • 多图像合成:组合多个图像
  • 迭代优化:逐步调整
  • 高保真文本:图像中的精确文本渲染
  • SynthID水印:所有生成的图像都包含水印

纵横比

支持的宽高比及其分辨率:

纵横比 分辨率 用例
1:1 1024x1024 方形图片,社交媒体
2:3 832x1248 肖像摄影
3:2 1248x832 风景摄影
3:4 864x1184 人像模式
4:3 1184x864 标准显示
4:5 896x1152 Instagram肖像
5:4 1152x896 中等格式
9:16 768x1344 垂直视频,故事
16:9 1344x768 宽屏,演示文稿
21:9 1536x672 超宽,电影级

出版

当推送新的版本标签时,该包会自动发布到npm:

# Update version in package.json, then:
git tag v0.1.0
git push origin v0.1.0

GitHub Actions工作流将:

  1. 跑linting
  2. 构建项目
  3. 运行集成测试
  4. 发布到npm(如果推送版本标签)

API成本

使用Gemini 2.5 Flash Image生成图像是基于令牌的:

  • 每100万个图像输出代币30美元
  • 每个图像输出都以1290个标记进行标记(统一速率,高达1024x1024px)
  • 每张图片约0.039美元

局限性

  • 英语、西班牙语(MX)、日语、中文和印地语的最佳表现
  • 图像生成不支持音频或视频输入
  • 模型在最多3幅输入图像的合成中效果最佳
  • 上传欧洲经济区、瑞士和英国不支持的儿童图片
  • 所有生成的图像都包含SynthID水印

资源

许可证

麻省理工学院

贡献

欢迎投稿!请随时提交拉取请求。