POST https://airouter.xincache.cn/v1beta/models/gemini-2.5-flash-image:generateContent
图片生成(文本转图片)
curl
curl --location 'https://airouter.xincache.cn/v1beta/models/gemini-2.5-flash-image:generateContent' \
--header "x-goog-api-key: $XINNETMODEL_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"contents": [
{
"role": "user",
"parts": [
{
"text": "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme"
}
]
}
],
"generationConfig": {
"responseModalities": [
"TEXT",
"IMAGE"
]
}
}' | jq -r '.candidates[0].content.parts[] | select(.inlineData and (.thought | not)) | .inlineData.data' | head -1 \
| base64 -d > xinnetmodel_generated_image.png
python
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
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"
),
)
contents = [
{
"text": "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme.",
},
]
generation_config = types.GenerationConfig(
response_modalities=["text", "image"],
)
response = client.models.generate_content(
model="gemini-2.5-flash-image",
contents=contents,
config={
"response_modalities": ["text", "image"],
},
)
for part in response.candidates[0].content.parts:
# 跳过思考过程中的中间图片,只保留最终输出图片
if getattr(part, "thought", False):
continue
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("xinnetmodel_generated_image.png")
图片编辑(文本和图片转图片)
curl
cat <<EOF | curl -X POST \
--header "Authorization: Bearer ${XINNETMODEL_API_KEY}" \
--header "Content-Type: application/json" \
--data @- \
https://airouter.xincache.cn/v1beta/models/gemini-2.5-flash-image:generateContent | jq -r '.candidates[0].content.parts[] | select(.inlineData and (.thought | not)) | .inlineData.data' | head -1 | base64 --decode > xinnetmodel_generated_image.png
{
"contents": [
{
"role": "user",
"parts": [
{"text": "Convert this photo to black and white, in a cartoonish style."},
{
"inlineData": {
"mimeType": "image/jpeg",
"data": "$(curl -s https://static.xinnet.com/xinnetmodel/xinnet-maxcot.jpg | base64 | tr -d '\n')"
}
}
]
}
],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"]
}
}
EOF
python
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
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"
),
)
response = client.models.generate_content(
model="gemini-2.5-flash-image",
contents=[
types.Content(
role="user",
parts=[
types.Part(
text="Convert this photo to black and white, in a cartoonish style."
),
types.Part(
file_data=types.FileData(
mime_type="image/jpeg",
file_uri="https://static.xinnet.com/xinnetmodel/xinnet-maxcot.jpg",
)
),
],
)
],
config=types.GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]),
)
for part in response.candidates[0].content.parts:
# 跳过思考过程中的中间图片,只保留最终输出图片
if getattr(part, "thought", False):
continue
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("xinnetmodel_generated_image.png")