Nano Banana 图像生成
通过 211API 直接调用 Gemini 原生图像生成模型,无需 Google 账号,一个 API Key 即可出图。
模型选择
| 模型 | 模型 ID | 特点 | 适用场景 |
|---|---|---|---|
| Nano Banana Pro | gemini-3-pro-image-preview | 画质最佳,支持高清输出 | 海报、商业素材、精细创作 |
| Nano Banana 2 | gemini-3.1-flash-image-preview | 速度快、价格低 | 快速预览、批量生成、日常使用 |
提示
模型 ID 以 211API 控制台「模型列表」为准;追求画质选 Pro,追求速度与性价比选 Flash 版。
宽高比与分辨率
两个模型均支持 10 种宽高比,每种可选 1K / 2K / 4K 三档分辨率:
| 宽高比 | 1K | 2K(推荐) | 4K |
|---|---|---|---|
| 1:1 | 1024×1024 | 2048×2048 | 4096×4096 |
| 16:9 | 1376×768 | 2752×1536 | 5504×3072 |
| 9:16 | 768×1376 | 1536×2752 | 3072×5504 |
| 4:3 | 1200×896 | 2400×1792 | 4800×3584 |
| 3:4 | 896×1200 | 1792×2400 | 3584×4800 |
| 3:2 | 1232×816 | 2464×1632 | 4928×3264 |
| 2:3 | 816×1232 | 1632×2464 | 3264×4928 |
| 21:9 | 1584×672 | 3168×1344 | 6336×2688 |
| 5:4 | 1136×896 | 2272×1792 | 4544×3584 |
| 4:5 | 896×1136 | 1792×2272 | 3584×4544 |
API 请求格式
图像生成使用 Google 原生格式(generateContent),不是 OpenAI 兼容格式,注意区分。
端点:
POST https://www.211api.com/v1beta/models/{模型ID}:generateContent请求体:
json
{
"contents": [{
"parts": [
{ "text": "你的图片描述" }
]
}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {
"aspectRatio": "16:9",
"image_size": "2K"
}
}
}| 参数 | 说明 | 可选值 |
|---|---|---|
responseModalities | 响应类型,必须为图片 | ["IMAGE"] |
aspectRatio | 宽高比 | 1:1、16:9、9:16、4:3、3:4、3:2、2:3、21:9、5:4、4:5 |
image_size | 分辨率档位 | 1K、2K、4K |
示例代码
cURL
bash
curl -X POST "https://www.211api.com/v1beta/models/gemini-3-pro-image-preview:generateContent" \
-H "Authorization: Bearer sk-你的-211API-Key" \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"parts": [
{"text": "一只可爱的小猫咪坐在花园里,油画风格,高清,细节丰富"}
]
}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {
"aspectRatio": "16:9",
"image_size": "2K"
}
}
}'Python
python
import base64
import requests
API_KEY = "sk-你的-211API-Key"
API_URL = "https://www.211api.com/v1beta/models/gemini-3-pro-image-preview:generateContent"
payload = {
"contents": [{
"parts": [{"text": "一只可爱的小猫咪坐在花园里,油画风格,高清,细节丰富"}]
}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {"aspectRatio": "16:9", "image_size": "2K"}
}
}
resp = requests.post(
API_URL,
headers={"Authorization": f"Bearer {API_KEY}"},
json=payload,
timeout=600,
)
if resp.status_code == 200:
data = resp.json()
image_b64 = data["candidates"][0]["content"]["parts"][0]["inlineData"]["data"]
with open("output.png", "wb") as f:
f.write(base64.b64decode(image_b64))
print("图片已保存为 output.png")
else:
print(f"请求失败: {resp.status_code}", resp.text)Node.js
javascript
const fs = require('fs')
const API_KEY = 'sk-你的-211API-Key'
const API_URL =
'https://www.211api.com/v1beta/models/gemini-3-pro-image-preview:generateContent'
async function generateImage() {
const resp = await fetch(API_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
contents: [{
parts: [{ text: '一只可爱的小猫咪坐在花园里,油画风格,高清,细节丰富' }]
}],
generationConfig: {
responseModalities: ['IMAGE'],
imageConfig: { aspectRatio: '16:9', image_size: '2K' }
}
})
})
const data = await resp.json()
const imageB64 = data.candidates[0].content.parts[0].inlineData.data
fs.writeFileSync('output.png', Buffer.from(imageB64, 'base64'))
console.log('图片已保存为 output.png')
}
generateImage().catch(console.error)图生图编辑
Pro 模型支持上传图片并按指令修改:把图片 Base64 放入 inlineData.data,再加上文字指令即可。
json
{
"contents": [{
"parts": [
{
"inlineData": {
"mimeType": "image/png",
"data": "图片的Base64编码字符串"
}
},
{ "text": "将背景改为星空,保持人物不变" }
]
}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": { "aspectRatio": "16:9", "image_size": "2K" }
}
}编辑指令尽量具体:说清楚保留什么、修改什么。支持换背景、改风格、加元素等操作。
性能建议
- 超时设置:1K 约 360 秒、2K 约 600 秒(推荐)、4K 约 1200 秒,分辨率越高耗时越长。
- 带宽:图片以 Base64 传输,4K 图片数据可能超过 10MB,请使用稳定高速的网络。
注意事项
- API Key 需在 211API 控制台 创建,并确认有图像模型的使用权限。
- 支持在图片中渲染中文文字(招牌、海报文案等),在 prompt 中直接写明即可。
- 不同模型与分辨率价格不同,以控制台定价为准。