Gemini API 现已支持强大的图像生成功能,其模型 gemini-2.5-flash-image-preview(也被称为 Nano Banana)允许用户通过对话方式创建和处理视觉内容。你可以结合文本、图像或两者来与模型交互,实现前所未有的创作控制力。
核心功能包括:
文本到图像 (Text-to-Image):根据简单或复杂的文本描述生成高质量图像。 图像编辑 (Image + Text-to-Image):提供一张现有图像,并用文本指令来添加、移除或修改其中的元素,调整风格或色彩。 图像合成与风格迁移 (Multi-image to Image):利用多张输入图像合成一个新场景,或将一张图像的艺术风格应用到另一张上。 迭代式优化:通过连续对话逐步微调图像,直至达到理想效果。 高保真文本渲染:在图像中精准地生成清晰、可读且位置合理的文本,非常适合制作徽标、图表和海报。 所有通过此功能生成的图像都将包含 SynthID 数字水印。
文本到图像生成 (Text-to-Image) 这是最基础的用法,只需提供一段描述性的文本提示词,即可生成相应的图像。
以下代码演示了如何根据提示词“创建一张以 Gemini 为主题的高档餐厅中的‘纳米香蕉’菜肴图片”来生成图像。
Python from google import genai from PIL import Image from io import BytesIO # 需要先配置你的 API Key # genai.configure(api_key="YOUR_API_KEY") client = genai.Client() prompt = "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme" response = client.models.generate_content( model="gemini-2.5-flash-image-preview", contents=[prompt], ) # 响应中可能包含文本和图像部分 for part in response.candidates[0].content.parts: if part.text is not None: print(part.text) elif part.inline_data is not None: # 将图像数据保存为文件 image_data = part.inline_data.data image = Image.open(BytesIO(image_data)) image.save("generated_image.png") print("图像已保存为 generated_image.png") JavaScript (Node.js) import { GoogleGenAI } from "@google/genai"; import * as fs from "node:fs"; async function main() { // 需要先配置你的 API Key const genAI = new GoogleGenAI("YOUR_API_KEY"); const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash-image-preview" }); const prompt = "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme"; const result = await model.generateContent(prompt); const response = result.response; const candidate = response.candidates[0]; for (const part of candidate.content.parts) { if (part.text) { console.log(part.text); } else if (part.inlineData) { const imageData = part.inlineData.data; const buffer = Buffer.from(imageData, "base64"); fs.writeFileSync("generated_image.png", buffer); console.log("图像已保存为 generated_image.png"); } } } main(); Go package main import ( "context" "fmt" "log" "os" "google.golang.org/api/option" "google.golang.org/genai" ) func main() { ctx := context.Background() // 需要先配置你的 API Key client, err := genai.NewClient(ctx, option.WithAPIKey("YOUR_API_KEY")) if err != nil { log.Fatal(err) } defer client.Close() model := client.GenerativeModel("gemini-2.5-flash-image-preview") prompt := genai.Text("Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme") resp, err := model.GenerateContent(ctx, prompt) if err != nil { log.Fatal(err) } for _, part := range resp.Candidates[0].Content.Parts { if txt, ok := part.(genai.Text); ok { fmt.Println(txt) } else if data, ok := part.(genai.ImageData); ok { err := os.WriteFile("generated_image.png", data, 0644) if err != nil { log.Fatal(err) } fmt.Println("图像已保存为 generated_image.png") } } } REST API (cURL) curl -s -X POST " \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "contents": [{ "parts": [ {"text": "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme"} ] }] }' \ | grep -o '"data": "[^"]*"' \ | cut -d '"' -f4 \ | base64 --decode > generated_image.png 图像编辑 (图文到图像) 此功能允许你上传一张图片,并结合文本指令对其进行修改。
...