Imagen คือโมเดลการสร้างรูปภาพที่มีความเที่ยงตรงสูงของ Google ซึ่งสามารถสร้าง รูปภาพที่สมจริงและมีคุณภาพสูงจากพรอมต์ข้อความ รูปภาพที่สร้างขึ้นทั้งหมด จะมีลายน้ำ SynthID ดูข้อมูลเพิ่มเติมเกี่ยวกับโมเดล Imagen ที่มีให้ใช้งานได้ในส่วนเวอร์ชันโมเดล
การย้ายข้อมูลไปยัง Nano Banana
เราเลิกใช้งานโมเดล Imagen แล้วและจะปิดให้บริการในวันที่ 17 สิงหาคม 2026 เราขอแนะนำให้ ย้ายข้อมูลไปยัง Nano Banana เพื่อตอบสนองความต้องการในการสร้างรูปภาพ
การย้ายข้อมูลจะเกี่ยวข้องกับการเปลี่ยนแปลงต่อไปนี้
- ชื่อโมเดล: ใช้
gemini-2.5-flash-imageแทนชื่อโมเดล Imagen - วิธีการ: ใช้
client.models.generate_contentแทนclient.models.generate_images - การจัดการการตอบกลับ: Nano Banana จะแสดงผลชิ้นส่วนเนื้อหา ซึ่งอาจรวมถึงข้อมูลรูปภาพ แทนที่จะเป็นออบเจ็กต์การตอบกลับรูปภาพที่เฉพาะเจาะจง
ดูรายละเอียดและตัวอย่างเพิ่มเติมได้ที่คำแนะนำการสร้างรูปภาพ
สร้างรูปภาพโดยใช้โมเดล Imagen
ตัวอย่างนี้แสดงการสร้างรูปภาพด้วยโมเดล Imagen
Python
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
client = genai.Client()
response = client.models.generate_images(
model='imagen-4.0-generate-001',
prompt='Robot holding a red skateboard',
config=types.GenerateImagesConfig(
number_of_images= 4,
)
)
for generated_image in response.generated_images:
generated_image.image.show()
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
const response = await ai.models.generateImages({
model: 'imagen-4.0-generate-001',
prompt: 'Robot holding a red skateboard',
config: {
numberOfImages: 4,
},
});
let idx = 1;
for (const generatedImage of response.generatedImages) {
let imgBytes = generatedImage.image.imageBytes;
const buffer = Buffer.from(imgBytes, "base64");
fs.writeFileSync(`imagen-${idx}.png`, buffer);
idx++;
}
}
main();
Go
package main
import (
"context"
"fmt"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
config := &genai.GenerateImagesConfig{
NumberOfImages: 4,
}
response, _ := client.Models.GenerateImages(
ctx,
"imagen-4.0-generate-001",
"Robot holding a red skateboard",
config,
)
for n, image := range response.GeneratedImages {
fname := fmt.Sprintf("imagen-%d.png", n)
_ = os.WriteFile(fname, image.Image.ImageBytes, 0644)
}
}
REST
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"instances": [
{
"prompt": "Robot holding a red skateboard"
}
],
"parameters": {
"sampleCount": 4
}