Gemini 图像生成
gemini-3-pro-image 与 gemini-3.1-flash-image 的原生 generateContent 生图:宽高比与分辨率档位、图片编辑、多轮迭代、搜索接地与思考模式。全部经实测验证
POST /v1beta/models/{model}:generateContent
Gemini 系图像模型走 Gemini 原生协议。生图请务必用这个端点——走 Chat 兼容协议也能出图,
但 imageConfig(aspectRatio 画幅、imageSize 分辨率)会在协议转换中整个丢掉,只能拿到默认尺寸。
文本对话见 Gemini generateContent;GPT 生图见 GPT 图像 · Image API 与 GPT 图像 · Responses,Grok 见 Grok 图像。
基本调用
建议用官方 SDK(Python google-genai、Node.js @google/genai),base_url 填不带 /v1 的根地址。
curl -s -X POST \
"https://www.vibeapi.cn/v1beta/models/gemini-3.1-flash-image:generateContent" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"parts": [{"text": "一只可爱的猫咪在阳光下打盹"}]}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {"aspectRatio": "16:9", "imageSize": "1K"}
}
}' -o response.json
# 提取图片
jq -r '.candidates[0].content.parts[0].inlineData.data' response.json | base64 -d > output.pngfrom google import genai
from google.genai import types
client = genai.Client(
api_key="YOUR_API_KEY",
http_options=types.HttpOptions(base_url="https://www.vibeapi.cn"),
)
response = client.models.generate_content(
model="gemini-3.1-flash-image",
contents="一只可爱的猫咪在阳光下打盹",
config=types.GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=types.ImageConfig(aspect_ratio="16:9", image_size="1K"),
),
)
for part in response.parts:
if part.inline_data is not None:
part.as_image().save("cat.png")
breakimport { GoogleGenAI } from "@google/genai";
import fs from "fs";
const ai = new GoogleGenAI({
apiKey: "YOUR_API_KEY",
httpOptions: { baseUrl: "https://www.vibeapi.cn" },
});
const response = await ai.models.generateContent({
model: "gemini-3.1-flash-image",
contents: "一只可爱的猫咪在阳光下打盹",
config: {
responseModalities: ["IMAGE"],
imageConfig: { aspectRatio: "16:9", imageSize: "1K" },
},
});
for (const part of response.candidates[0].content.parts) {
if (part.inlineData) {
fs.writeFileSync("cat.png", Buffer.from(part.inlineData.data, "base64"));
break;
}
}以下内容均来自对本网关的真实调用,最后验证:2026-08-10。
可用模型
超时建议:512px / 1K 约 80 秒,2K 约 200 秒,4K 约 350 秒。
| 模型 | 适用场景 | 特点 |
|---|---|---|
gemini-3-pro-image-preview | 专业素材、复杂指令 | 高级推理、搜索接地、最高 4K、最多 14 张参考图 |
gemini-3.1-flash-image | 日常生成、批量任务 | 性价比高、支持 512px-4K、思考等级控制、图片搜索接地 |
请求格式
{
"contents": [
{
"parts": [
{ "text": "你的提示词" }
]
}
],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "1K"
}
}
}generationConfig 参数
| 参数 | 类型 | 说明 |
|---|---|---|
responseModalities | string[] | ["IMAGE"] 仅图片;["TEXT", "IMAGE"] 图文混合(默认) |
imageConfig.aspectRatio | string | 宽高比,见下方支持列表 |
imageConfig.imageSize | string | 分辨率档位:512px(仅 flash)、1K、2K、4K。必须大写 K |
支持的宽高比
全部 14 种,两个模型均已验证通过:
1:1 1:4 1:8 2:3 3:2 3:4 4:1 4:3 4:5 5:4 8:1 9:16 16:9 21:9
响应格式
{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{
"inlineData": {
"mimeType": "image/png",
"data": "<BASE64_IMAGE_DATA>"
}
}
]
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 10,
"candidatesTokenCount": 1120,
"totalTokenCount": 1130
}
}图片在 candidates[0].content.parts[].inlineData 中,base64 编码。
当 responseModalities 包含 "TEXT" 时,parts 中可能同时包含 text 和 inlineData。
分辨率参考表
gemini-3.1-flash-image
| 宽高比 | 512px | 1K | 2K | 4K |
|---|---|---|---|---|
| 1:1 | 512×512 | 1024×1024 | 2048×2048 | 4096×4096 |
| 1:4 | 256×1024 | 512×2064 | 1024×4128 | 2048×8256 |
| 1:8 | 176×1456 | 352×2928 | 704×5856 | 1408×11712 |
| 2:3 | 416×624 | 848×1264 | 1696×2528 | 3392×5056 |
| 3:2 | 624×416 | 1264×848 | 2528×1696 | 5056×3392 |
| 3:4 | 448×592 | 896×1200 | 1792×2400 | 3584×4800 |
| 4:1 | 1024×256 | 2064×512 | 4128×1024 | 8256×2048 |
| 4:3 | 592×448 | 1200×896 | 2400×1792 | 4800×3584 |
| 4:5 | 464×576 | 928×1152 | 1856×2304 | 3712×4608 |
| 5:4 | 576×464 | 1152×928 | 2304×1856 | 4608×3712 |
| 8:1 | 1456×176 | 2928×352 | 5856×704 | 11712×1408 |
| 9:16 | 384×688 | 768×1376 | 1536×2752 | 3072×5504 |
| 16:9 | 688×384 | 1376×768 | 2752×1536 | 5504×3072 |
| 21:9 | 784×336 | 1584×672 | 3168×1344 | 6336×2688 |
512px 档位仅 flash 模型支持。
gemini-3-pro-image-preview
支持 1K、2K、4K,不支持 512px。分辨率与 flash 的 1K/2K/4K 一致。
耗时参考
| 档位 | 典型耗时 |
|---|---|
| 512px | 10-17s |
| 1K | 13-40s |
| 2K | 40-170s |
| 4K | 120-310s |
图片编辑
原图与文字指令一起放进 contents,即为编辑;SDK 直接传 PIL Image,REST 用 inline_data 传 base64。
只改局部时,在指令里明确说清保留什么:
from PIL import Image
response = client.models.generate_content(
model="gemini-3.1-flash-image",
contents=[Image.open("cat.png"), "给这只猫戴上一顶圣诞帽"],
config=types.GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=types.ImageConfig(aspect_ratio="1:1", image_size="1K"),
),
)
for part in response.parts:
if part.inline_data is not None:
part.as_image().save("cat_hat.png")
break同一写法覆盖三类常见任务,只换指令:
| 任务 | 指令示例 |
|---|---|
| 局部重绘(语义遮盖) | Change only the background to a snowy winter scene. Keep the cat exactly the same. |
| 风格迁移 | Transform this photograph into the style of Van Gogh's Starry Night. Preserve the composition but render with swirling, impasto brushstrokes. |
| 多图合成 | contents 里放多张图 + 指令:让第二张图中的人穿上第一张图中的蓝色连衣裙,生成一张专业电商照片 |
REST 写法(多图合成):
curl -s -X POST \
"https://www.vibeapi.cn/v1beta/models/gemini-3.1-flash-image:generateContent" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"parts": [
{"text": "让第二张图中的人穿上第一张图中的蓝色连衣裙,生成一张专业电商照片"},
{"inline_data": {"mime_type": "image/png", "data": "<DRESS_BASE64>"}},
{"inline_data": {"mime_type": "image/png", "data": "<MODEL_BASE64>"}}
]}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {"aspectRatio": "3:4", "imageSize": "2K"}
}
}'多轮迭代
把上一轮模型返回的完整 parts(含 inlineData 与 thoughtSignature)以 role: "model" 原样放回 contents,再追加新指令:
history = [{"role": "user", "parts": [{"text": "画一只橘猫坐在窗台上"}]}]
r1 = client.models.generate_content(model="gemini-3.1-flash-image", contents=history,
config=types.GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]))
history.append(r1.candidates[0].content) # 模型回复原样入史
history.append({"role": "user", "parts": [{"text": "把背景改成下雨天"}]})
r2 = client.models.generate_content(model="gemini-3.1-flash-image", contents=history,
config=types.GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]))高级功能
Google 搜索接地
基于实时搜索数据生成图片(如天气、新闻、股票)。在请求中添加 tools 字段:
{
"contents": [{"parts": [{"text": "可视化旧金山今天的天气预报"}]}],
"tools": [{"google_search": {}}],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {"aspectRatio": "16:9"}
}
}响应中会额外返回 groundingMetadata,包含 webSearchQueries(搜索词)和 groundingChunks(来源链接)。
Flash 还额外支持图片搜索接地,可用网络图片作为视觉参考:
"tools": [{"google_search": {"search_types": {"web_search": {}, "image_search": {}}}}]思考模式
Pro 模型默认启用思考模式,会先生成构思草图再输出最终图片。响应中 part.thought == true 的为思考过程,可跳过。
Flash 模型支持控制思考等级(minimal 默认 或 high),通过 generationConfig.thinkingConfig 设置:
{
"contents": [{"parts": [{"text": "A futuristic city inside a glass bottle floating in space"}]}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"},
"thinkingConfig": {
"thinkingLevel": "high",
"includeThoughts": true
}
}
}high 模式下响应 parts 会包含多种类型:
| part 类型 | 说明 |
|---|---|
thought == true + text | 思考文本(推理过程) |
thought == true + inlineData | 构思草图(临时图片) |
inlineData(无 thought) | 最终输出图片 |
提取最终图片时跳过 thought parts:
for part in response.candidates[0].content.parts:
if getattr(part, "thought", False):
continue # 跳过思考过程
if part.get("inlineData"):
# 这是最终图片
save(part["inlineData"])无论
includeThoughts设为 true 还是 false,思考 token 都会计费。high模式会消耗更多 token 但图片质量更高。
多张参考图片
Pro 支持最多 6 张对象图 + 5 张人物图(共 14 张);Flash 支持最多 10 张对象图 + 4 张人物图。
能力边界
- imageSize 大小写:必须用大写
K(1K、2K、4K),小写1k会被拒绝 - 512px 仅 flash:Pro 模型不支持 512px 档位
- 超时:4K 分辨率生成可能需要 2-5 分钟,务必设置足够的超时
- Token 消耗:512px 约 747 token,1K 约 1120 token,4K 约 2000 token
- 默认行为:不传
imageConfig时,默认输出约 1408×768(接近 16:9 的 1K) - 图片格式:响应中
mimeType通常为image/png,偶尔为image/jpeg - SynthID 水印:所有生成图片均包含 SynthID 数字水印
- 推荐语言:英语、中文、日语、韩语、法语、德语、西班牙语等
在线调试
填入你自己的 API Key 即可直接发起请求,参数表与响应结构由接口定义生成。
Authorization
BearerAuth
使用 Bearer Token 认证。
格式: Authorization: Bearer sk-xxxxxx
In: header
Path Parameters
模型名称
Request Body
application/json
Response Body
application/json
curl -X POST "https://www.vibeapi.cn/v1beta/models/string:generateContent/" \ -H "Content-Type: application/json" \ -d '{ "contents": [ {} ], "generationConfig": { "responseModalities": [ "string" ], "imageConfig": { "aspectRatio": "string", "imageSize": "string" } } }'{
"candidates": [
{
"content": {
"role": "string",
"parts": [
{}
]
},
"finishReason": "string",
"safetyRatings": [
{}
]
}
],
"usageMetadata": {
"promptTokenCount": 0,
"candidatesTokenCount": 0,
"totalTokenCount": 0
}
}