主题
gemini-3.1-flash-image-preview 图片编辑
POST
https://az.gptplus5.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent
官方文档: https://ai.google.dev/gemini-api/docs/image-generation?hl=zh-cn#rest
提供图片并使用文本提示添加、移除或修改元素、更改样式或调整色彩分级。以下示例演示了如何上传 base64 编码的图片。如需了解多张图片、更大的载荷和支持的 MIME 类型,请参阅图片理解页面。
提醒:请确保您对上传的所有图片均拥有必要权利。请勿生成会侵犯他人权利的内容,包括会欺骗、骚扰或伤害他人的视频或图片。使用此生成式 AI 服务时须遵守《使用限制政策》。
请求参数
Header 参数
| 参数名 | 类型 | 必需 | 说明 | 示例 |
|---|---|---|---|---|
Authorization | string | 必需 | API Key 鉴权 | Bearer {{YOUR_API_KEY}} |
Content-Type | string | 必需 | application/json |
Body 参数 (application/json)
| 参数名 | 类型 | 必需 | 说明 |
|---|---|---|---|
contents | array | 必需 | 多轮对话内容数组,每一项代表一个轮次 |
└ parts | array | 必需 | 当前轮次的内容片段数组,可包含文本和图片 |
└ text | string | 可选 | 文本提示词,描述要对图片做的修改(添加/移除/替换元素,更改样式或调色等) |
└ inlineData | object | 可选 | 内联图片数据,与 text 同级,可在 parts 中混合传入多个 |
└ mimeType | string | 必需 | 图片 MIME 类型,如 image/jpeg、image/png |
└ data | string | 必需 | 图片的 Base64 编码字符串(不要带 data:image/...;base64, 前缀) |
请求示例
json
{
"contents": [
{
"parts": [
{
"text": "Create a picture of my cat eating a nano-banana in a fancy restaurant under the Gemini constellation"
},
{
"inlineData": {
"mimeType": "image/jpeg",
"data": "<BASE64_IMAGE_DATA>"
}
}
]
}
]
}cURL 示例
bash
curl --location --request POST 'https://az.gptplus5.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-xxx' \
--data-raw '{
"contents": [
{
"parts": [
{
"text": "Create a picture of my cat eating a nano-banana in a fancy restaurant under the Gemini constellation"
},
{
"inlineData": {
"mimeType": "image/jpeg",
"data": "<BASE64_IMAGE_DATA>"
}
}
]
}
]
}'多轮图片修改
继续以对话方式生成和修改图片。建议通过多轮对话来迭代优化图片。将上一轮的模型输出图片(inlineData)作为下一轮 contents 中的输入图片再次传入,并附上新的文本指令,即可对图片进行连续修改。
请求示例(多轮)
json
{
"contents": [
{
"role": "user",
"parts": [
{
"text": "Create a vibrant infographic that explains photosynthesis as if it were a recipe for a plants favorite food."
}
]
},
{
"role": "model",
"parts": [
{
"inlineData": {
"mimeType": "image/jpeg",
"data": "<PREVIOUS_BASE64_IMAGE_DATA>"
}
}
]
},
{
"role": "user",
"parts": [
{
"text": "Update this infographic to be in Spanish. Do not change any other elements of the image."
}
]
}
]
}返回响应
🟢200成功
响应 Body
| 参数名 | 类型 | 必需 | 说明 |
|---|---|---|---|
candidates | array | 必需 | 候选结果列表(通常取 candidates[0]) |
candidates[0].content.parts | array | 必需 | 实际业务输出内容 |
└ inlineData.mimeType | string | 必需 | 输出内容类型(如 image/jpeg) |
└ inlineData.data | string | 必需 | 输出图片的 Base64 编码字符串 |
响应示例
json
{
"candidates": [
{
"content": {
"parts": [
{
"inlineData": {
"mimeType": "image/jpeg",
"data": "<base64...>"
}
}
]
}
}
]
}