POST https://airouter.xincache.cn/v1beta/models/gemini-3-pro-image-preview:generateContent
图片生成(文本转图片)
curl
curl -s -X POST \
"https://airouter.xincache.cn/v1beta/models/gemini-3-pro-image-preview:generateContent" \
-H "x-goog-api-key: $XINNETMODEL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"parts": [{"text": "Da Vinci style anatomical sketch of a dissected Monarch butterfly. Detailed drawings of the head, wings, and legs on textured parchment with notes in English."}]}],
"tools": [{"google_search": {}}],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"}
}
}' | jq -r '.candidates[0].content.parts[] | select(.inlineData and (.thought | not)) | .inlineData.data' | head -1 | base64 --decode > butterfly.png
python
from google import genai
from google.genai import types
import os
client = genai.Client(
api_key=os.getenv("XINNETMODEL_API_KEY", "<XINNETMODEL_API_KEY>"), # 您的API_KEY
http_options=types.HttpOptions(
base_url="https://airouter.xincache.cn"
),
)
prompt = "Da Vinci style anatomical sketch of a dissected Monarch butterfly. Detailed drawings of the head, wings, and legs on textured parchment with notes in English."
aspect_ratio = "1:1" # "1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9","21:9"
resolution = "1K" # "1K", "2K", "4K"
response = client.models.generate_content(
model="gemini-3-pro-image-preview",
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=["TEXT", "IMAGE"],
image_config=types.ImageConfig(
aspect_ratio=aspect_ratio, image_size=resolution
),
),
)
for part in response.parts:
# 跳过思考过程中的中间图片,只保留最终输出图片
if getattr(part, "thought", False):
continue
if part.text is not None:
print(part.text)
elif image := part.as_image():
image.save("butterfly.png")
响应示例
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Here is the anatomical sketch of a dissected Monarch butterfly in Da Vinci style..."
},
{
"inlineData": {
"data": "iVBORw0KGgoAAAANSUhEUgAA...",
"mimeType": "image/png"
}
}
],
"role": "model"
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"candidatesTokenCount": 1315,
"totalTokenCount": 1331
}
}