Gemini สามารถสร้างและประมวลผลรูปภาพในรูปแบบการสนทนาได้ คุณสามารถป้อนพรอมต์ให้ Gemini ด้วยข้อความ รูปภาพ หรือทั้ง 2 อย่างรวมกันเพื่อทำงานต่างๆ ที่เกี่ยวข้องกับรูปภาพ เช่น การสร้างและการแก้ไขรูปภาพ รูปภาพที่สร้างขึ้นทั้งหมดจะมีลายน้ำ SynthID
การสร้างรูปภาพอาจไม่พร้อมให้บริการในบางภูมิภาคและบางประเทศ โปรดดูข้อมูลเพิ่มเติมที่หน้าโมเดล Gemini
การสร้างรูปภาพ (ข้อความเป็นรูปภาพ)
โค้ดต่อไปนี้แสดงวิธีสร้างรูปภาพโดยอิงตามพรอมต์เชิงพรรณนา
คุณต้องระบุ responseModalities: ["TEXT", "IMAGE"] ในการกำหนดค่า
โม��ดลเหล่านี้ไม่รองรับเอาต์พุตที่เป็นรูปภาพเท่านั้น
Python
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import base64
client = genai.Client()
contents = ('Hi, can you create a 3d rendered image of a pig '
'with wings and a top hat flying over a happy '
'futuristic scifi city with lots of greenery?')
response = client.models.generate_content(
model="gemini-2.0-flash-preview-image-generation",
contents=contents,
config=types.GenerateContentConfig(
response_modalities=['TEXT', 'IMAGE']
)
)
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 = Image.open(BytesIO((part.inline_data.data)))
image.save('gemini-native-image.png')
image.show()
JavaScript
import { GoogleGenAI, Modality } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
const contents =
"Hi, can you create a 3d rendered image of a pig " +
"with wings and a top hat flying over a happy " +
"futuristic scifi city with lots of greenery?";
// Set responseModalities to include "Image" so the model can generate an image
const response = await ai.models.generateContent({
model: "gemini-2.0-flash-preview-image-generation",
contents: contents,
config: {
responseModalities: [Modality.TEXT, Modality.IMAGE],
},
});
for (const part of response.candidates[0].content.parts) {
// Based on the part type, either show the text or save the image
if (part.text) {
console.log(part.text);
} else if (part.inlineData) {
const imageData = part.inlineData.data;
const buffer = Buffer.from(imageData, "base64");
fs.writeFileSync("gemini-native-image.png", buffer);
console.log("Image saved as gemini-native-image.png");
}
}
}
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.GenerateContentConfig{
ResponseModalities: []string{"TEXT", "IMAGE"},
}
result, _ := client.Models.GenerateContent(
ctx,
"gemini-2.0-flash-preview-image-generation",
genai.Text("Hi, can you create a 3d rendered image of a pig " +
"with wings and a top hat flying over a happy " +
"futuristic scifi city with lots of greenery?"),
config,
)
for _, part := range result.Candidates[0].Content.Parts {
if part.Text != "" {
fmt.Println(part.Text)
} else if part.InlineData != nil {
imageBytes := part.InlineData.Data
outputFilename := "gemini_generated_image.png"
_ = os.WriteFile(outputFilename, imageBytes, 0644)
}
}
}
REST
curl -s -X POST
"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-preview-image-generation:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"parts": [
{"text": "Hi, can you create a 3d rendered image of a pig with wings and a top hat flying over a happy futuristic scifi city with lots of greenery?"}
]
}],
"generationConfig":{"responseModalities":["TEXT","IMAGE"]}
}' \
| grep -o '"data": "[^"]*"' \
| cut -d'"' -f4 \
| base64 --decode > gemini-native-image.png
การแก้ไขรูปภาพ (ข้อความและรูปภาพเป็นรูปภาพ)
หากต้องการแก้ไขรูปภาพ ให้เพิ่มรูปภาพเป็นอินพุต ตัวอย่างต่อไปนี้ แสดงการอัปโหลดรูปภาพที่เข้ารหัส Base64 สำหรับรูปภาพหลายรูปและเพย์โหลดขนาดใหญ่ ให้ดูส่วนimage input
Python
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import PIL.Image
image = PIL.Image.open('/path/to/image.png')
client = genai.Client()
text_input = ('Hi, This is a picture of me.'
'Can you add a llama next to me?',)
response = client.models.generate_content(
model="gemini-2.0-flash-preview-image-generation",
contents=[text_input, image],
config=types.GenerateContentConfig(
response_modalities=['TEXT', 'IMAGE']
)
)
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 = Image.open(BytesIO((part.inline_data.data)))
image.show()
JavaScript
import { GoogleGenAI, Modality } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
// Load the image from the local file system
const imagePath = "path/to/image.png";
const imageData = fs.readFileSync(imagePath);
const base64Image = imageData.toString("base64");
// Prepare the content parts
const contents = [
{ text: "Can you add a llama next to the image?" },
{
inlineData: {
mimeType: "image/png",
data: base64Image,
},
},
];
// Set responseModalities to include "Image" so the model can generate an image
const response = await ai.models.generateContent({
model: "gemini-2.0-flash-preview-image-generation",
contents: contents,
config: {
responseModalities: [Modality.TEXT, Modality.IMAGE],
},
});
for (const part of response.candidates[0].content.parts) {
// Based on the part type, either show the text or save the image
if (part.text) {
console.log(part.text);
} else if (part.inlineData) {
const imageData = part.inlineData.data;
const buffer = Buffer.from(imageData, "base64");
fs.writeFileSync("gemini-native-image.png", buffer);
console.log("Image saved as gemini-native-image.png");
}
}
}
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)
}
imagePath := "/path/to/image.png"
imgData, _ := os.ReadFile(imagePath)
parts := []*genai.Part{
genai.NewPartFromText("Hi, This is a picture of me. Can you add a llama next to me?"),
&genai.Part{
InlineData: &genai.Blob{
MIMEType: "image/png",
Data: imgData,
},
},
}
contents := []*genai.Content{
genai.NewContentFromParts(parts, genai.RoleUser),
}
config := &genai.GenerateContentConfig{
ResponseModalities: []string{"TEXT", "IMAGE"},
}
result, _ := client.Models.GenerateContent(
ctx,
"gemini-2.0-flash-preview-image-generation",
contents,
config,
)
for _, part := range result.Candidates[0].Content.Parts {
if part.Text != "" {
fmt.Println(part.Text)
} else if part.InlineData != nil {
imageBytes := part.InlineData.Data
outputFilename := "gemini_generated_image.png"
_ = os.WriteFile(outputFilename, imageBytes, 0644)
}
}
}
REST
IMG_PATH=/path/to/your/image1.jpeg
if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
B64FLAGS="--input"
else
B64FLAGS="-w0"
fi
IMG_BASE64=$(base64 "$B64FLAGS" "$IMG_PATH" 2>&1)
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-preview-image-generation:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d "{
\"contents\": [{
\"parts\":[
{\"text\": \"'Hi, This is a picture of me. Can you add a llama next to me\"},
{
\"inline_data\": {
\"mime_type\":\"image/jpeg\",
\"data\": \"$IMG_BASE64\"
}
}
]
}],
\"generationConfig\": {\"responseModalities\": [\"TEXT\", \"IMAGE\"]}
}" \
| grep -o '"data": "[^"]*"' \
| cut -d'"' -f4 \
| base64 --decode > gemini-edited-image.png
โหมดการสร้างรูปภาพอื่นๆ
Gemini รองรับโหมดการโต้ตอบกับรูปภาพอื่นๆ ตามโครงสร้างพรอมต์และบริบท ซึ่งรวมถึง
- ข้อความเป็นรูปภาพและข้อความ (แทรกสลับ): แสดงรูปภาพพร้อมข้อความที่เกี่ยวข้อง
- ตัวอย่างพรอมต์: "สร้างสูตรอาหารภาพประกอบสำหรับปาเอญ่า"
- รูปภาพและข้อความเป็นรูปภาพและข้อความ (แทรกสลับ): ใช้รูปภาพและข้อความที่ป้อนเพื่อสร้างรูปภาพและข้อความใหม่ที่เกี่ยวข้อง
- ตัวอย่างพรอมต์: (พร้อมรูปภาพห้องที่มีเฟอร์นิเจอร์) "โซฟาสีอื่นจะเข้ากับพื้นที่ของฉันไหม อัปเดตรูปภาพได้ไหม"
- การแต่งรูปภาพแบบหลายรอบ (แชท): สร้าง / แต่งรูปภาพต่อไปแบบสนทนา
- ตัวอย่างพรอมต์: [อัปโหลดรูปภาพรถสีน้ำเงิน] "เปลี่ยนรถคันนี้เป็นรถเปิดประทุน", "ตอนนี้เปลี่ยนสีเป็นสีเหลือง"
ข้อจำกัด
- ใช้ภาษาต่อไปนี้เพื่อประสิทธิภาพที่ดีที่สุด: อังกฤษ, สเปน (เม็กซิโก), ญี่ปุ่น, จีน ฮินดี (อินเดีย)
- การสร้างรูปภาพไม่รองรับอินพุตเสียงหรือวิดีโอ
- การสร้างรูปภาพอาจไม่ทำงานในกรณีต่อไปนี้เสมอไป
- โมเดลอาจแสดงผลเป็นข้อความเท่านั้น ลองขอเอาต์พุตรูปภาพอย่างชัดเจน (เช่น "สร้างรูปภาพ" "แสดงรูปภาพไปพร้อมๆ กัน" "อัปเดตรูปภาพ")
- โมเดลอาจหยุดสร้างกลางคัน โปรดลองอีกครั้งหรือลองใช้ พรอมต์อื่น
- เมื่อสร้างข้อความสำหรับรูปภาพ Gemini จะทำงานได้ดีที่สุดหากคุณสร้างข้อความก่อน แล้วจึงขอรูปภาพที่มีข้อความนั้น
- การสร้างรูปภาพไม่พร้อมใช้งานในบางภูมิภาค/ประเทศ ดูข้อมูลเพิ่มเติมได้ที่โมเดล
กรณีที่ควรใช้ Imagen
นอกเหนือจากการใช้ความสามารถในการสร้างรูปภาพในตัวของ Gemini แล้ว คุณยังเข้าถึง Imagen ซึ่งเป็นโมเดลการสร้างรูปภาพเฉพาะทางของเราผ่าน Gemini API ได้ด้วย
เลือก Gemini เมื่อ
- คุณต้องมีรูปภาพที่เกี่ยวข้องตามบริบทซึ่งใช้ความรู้และการให้เหตุผลเกี่ยวกับโลก
- การผสานข้อความและรูปภาพอย่างแนบเนียนเป็นสิ่งสำคัญ
- คุณต้องการภาพที่ถูกต้องซึ่งฝังอยู่ในลำดับข้อความยาว
- คุณต้องการแก้ไขรูปภาพแบบสนทนาในขณะที่ยังคงบริบทไว้
เลือก Imagen เมื่อ
- คุณภาพของรูปภาพ ความสมจริงเหมือนภาพถ่าย รายละเอียดทางศิลปะ หรือสไตล์เฉพาะ (เช่น อิมเพรสชันนิสม์ อะนิเมะ) เป็นสิ่งสำคัญอันดับแรก
- การทำงานแก้ไขเฉพาะทาง เช่น การอัปเดตพื้นหลังผลิตภัณฑ์หรือการเพิ่มขนาดรูปภาพ
- การใส่การสร้างแบรนด์ สไตล์ หรือการสร้างโลโก้และการออกแบบผลิตภัณฑ์
Imagen 4 ควรเป็นโมเดลที่คุณเลือกใช้เมื่อเริ่มสร้างรูปภาพด้วย Imagen เลือก Imagen 4 Ultra สำหรับกรณีการใช้งานขั้นสูงหรือ เมื่อต้องการรูปภาพคุณภาพดีที่สุด โปรดทราบว่า Imagen 4 Ultra สร้างรูปภาพได้ครั้งละ 1 รูปเท่านั้น
ขั้นตอนถัดไป
- ดูคู่มือ Veo เพื่อดูวิธีสร้าง วิดีโอด้วย Gemini API
- ดูข้อมูลเพิ่มเติมเกี่ยวก��บโมเดล Gemini ได้ที่ โมเดล Gemini และ โมเดลทดลอง

