NanoBanana Pro Edit
Chỉnh sửa ảnh dùng cùng Gemini API (generateContent) như NanoBanana Pro khi tạo ảnh. Bạn gửi ảnh gốc + hướng dẫn chỉnh sửa trong yêu cầu và nhận ảnh đã chỉnh trong phản hồi. Xem Gemini docs: Image editing.
Endpoint: Giống tạo ảnh: POST https://aiberm.com/v1beta/models/gemini-3-pro-image-preview:generateContent.
Điểm chính: Đặt một phần ảnh (inlineData) và một phần văn bản (hướng dẫn chỉnh sửa) trong contents; đặt generationConfig.responseModalities thành ["TEXT", "IMAGE"] để nhận ảnh đã chỉnh; đọc kết quả từ candidates[0].content.parts → inline_data.
Ví dụ mã
Python dùng snake_case; REST/cURL dùng camelCase. Cùng ý nghĩa.
1from google import genai2from google.genai import types3from PIL import Image4from io import BytesIO5 6base_url = "https://aiberm.com"7api_key = "YOUR_API_KEY"8model = "gemini-3-pro-image-preview"9output_file = "edited.png"10 11image_path = "original.png"12edit_prompt = "Add a stylish top hat to this image, keep the rest unchanged."13 14client = genai.Client(15 api_key=api_key,16 http_options=types.HttpOptions(api_version="v1beta", base_url=base_url),17)18image = Image.open(image_path)19 20response = client.models.generate_content(21 model=model,22 contents=[edit_prompt, image],23 config=types.GenerateContentConfig(24 response_modalities=["TEXT", "IMAGE"],25 ),26)27 28for part in response.parts:29 if part.text is not None:30 print(part.text)31 elif part.inline_data is not None:32 Image.open(BytesIO(part.inline_data.data)).save(output_file)33 print(f"Saved edited image: {output_file}")Chỉnh sửa so với tạo ảnh
| Tạo ảnh (NanoBanana Pro) | Chỉnh sửa (trang này) | |
|---|---|---|
contents | Chỉ văn bản (mô tả ảnh cần tạo) | Ảnh + văn bản (mô tả cách chỉnh) |
generationConfig.imageConfig | Bắt buộc (tỷ lệ khung hình, 1K/2K/4K) | Tùy chọn khi chỉnh sửa |
responseModalities | ["IMAGE"] hoặc ["TEXT","IMAGE"] | Thường là ["TEXT","IMAGE"] |
Cùng endpoint và xác thực; chỉ khác contents và việc bạn có truyền imageConfig hay không. Để biết thêm tham số, xem NanoBanana Pro.
Nhiều ảnh
Đặt nhiều phần ảnh trong contents.parts (một inlineData cho mỗi ảnh), rồi một phần văn bản chứa hướng dẫn chỉnh sửa. Mô hình dùng tất cả ảnh và hướng dẫn để tạo kết quả (ví dụ “ghép chủ thể từ ảnh thứ nhất lên nền của ảnh thứ hai”).
Python: truyền nhiều đối tượng ảnh và một văn bản trong contents.
image1 = Image.open("photo.png")
image2 = Image.open("background.png")
response = client.models.generate_content(
model=model,
contents=["Place the person from the first image onto the background of the second.", image1, image2],
config=types.GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]),
)
cURL: trong contents[0].parts, thêm một inlineData cho mỗi ảnh, rồi một text.
"contents": [{
"parts": [
{ "inlineData": { "mimeType": "image/png", "data": "<BASE64_IMAGE1>" }},
{ "inlineData": { "mimeType": "image/png", "data": "<BASE64_IMAGE2>" }},
{ "text": "Place the person from the first image onto the background of the second." }
]
}]
Dùng URL ảnh
API chỉ chấp nhận inlineData (base64) hoặc fileData (qua File API); không chấp nhận URL ảnh trực tiếp. Với ảnh ở URL, hãy tải xuống trước rồi đưa vào yêu cầu.
Python: tải từ URL và truyền ảnh cho SDK (SDK sẽ mã hóa khi cần).
import requests
from io import BytesIO
image_url = "https://example.com/photo.jpg"
resp = requests.get(image_url)
image = Image.open(BytesIO(resp.content))
response = client.models.generate_content(
model=model,
contents=["Add a top hat to this image.", image],
config=types.GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]),
)
cURL: tải ảnh từ URL, chuyển thành base64, rồi đặt kết quả vào inlineData.data.