图像API接口
Text-to-image (文生图)
根据文本描述生成图像的API接口,根据用户提供的文本描述,使用AI技术生成相应的图像内容。支持高质量图像生成,适合创意设计和内容创作需求
📋 基本信息
| 属性 | 值 |
|---|---|
| 接口地址 | POST /v1/chat/completions |
| 模型名称 | gemini-2.5-flash-image |
📝 请求参数
| 路径 | 参数 | 类型 | 必填 | 说明 | 默认 |
|---|---|---|---|---|---|
| - | model | string | ✅ | 模型名称,固定为 gemini-2.5-flash-image | - |
| - | messages | array | ✅ | 消息数组,包含用户输入的文本 | - |
messages[] | role | string | ✅ | 消息角色,固定为 user | - |
messages[] | content | string | ✅ | 文本内容,描述要生成的图片 | - |
注意:
- text-to-image 模型仅需文本描述即可生成图片。
- 默认会保持输出图片与输入图片尺寸一致;若未提供输入图片,则生成 1:1 的方图。
- 若需指定宽高比,可通过
extra_body.google.image_config.aspect_ratio选择以下预设值。
| 宽高比 | 分辨率 |
|---|---|
| 1:1 | 1024x1024 |
| 2:3 | 832x1248 |
| 3:2 | 1248x832 |
| 3:4 | 864x1184 |
| 4:3 | 1184x864 |
| 4:5 | 896x1152 |
| 5:4 | 1152x896 |
| 9:16 | 768x1344 |
| 16:9 | 1344x768 |
| 21:9 | 1536x672 |
📝 请求示例
curl https://api.xty.app/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxx" \
-d '{
"model": "gemini-2.5-flash-image",
"messages": [
{
"role": "user",
"content": "一只可爱的橙色小猫坐在彩虹桥上,卡通风格,明亮的色彩,高质量,详细"
}
],
"extra_body": {
"google": {
"image_config": {
"aspect_ratio": "16:9"
}
}
}
}'import requests
import json
url = "https://api.xty.app/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer sk-xxxxx"
}
data = {
"model": "gemini-2.5-flash-image",
"messages": [
{
"role": "user",
"content": "一只可爱的橙色小猫坐在彩虹桥上,卡通风格,明亮的色彩,高质量,详细"
}
],
"extra_body": {
"google": {
"image_config": {
"aspect_ratio": "16:9"
}
}
}
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())from openai import OpenAI
import httpx
client = OpenAI(
base_url="https://api.xty.app/v1",
api_key="sk-xxxxx",
http_client=httpx.Client(
base_url="https://api.xty.app/v1",
follow_redirects=True,
),
)
response = client.chat.completions.create(
model="gemini-2.5-flash-image",
messages=[
{
"role": "user",
"content": "一只可爱的橙色小猫坐在彩虹桥上,卡通风格,明亮的色彩,高质量,详细"
}
],
extra_body={
"extra_body": {
"google": {
"image_config": {
"aspect_ratio": "16:9"
}
}
}
}
)
print(response)fetch("https://api.xty.app/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer sk-xxxxx"
},
body: JSON.stringify({
model: "gemini-2.5-flash-image",
messages: [
{
role: "user",
content: "一只可爱的橙色小猫坐在彩虹桥上,卡通风格,明亮的色彩,高质量,详细"
}
],
"extra_body": {
"google": {
"image_config": {
"aspect_ratio": "16:9"
}
}
}
})
})
.then(response => response.json())
.then(data => console.log(data));📤 响应示例
成功响应
{
"id": "gemini-1704067200000",
"object": "chat.completion",
"created": 1704067200,
"model": "gemini-2.5-flash-image",
"choices": [
{
"index": 0,
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "这是您的小猫:"
}
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
}
}错误响应
{
"error": {
"message": "请求失败",
"type": "invalid_request_error",
"param": null,
"code": null
}
}✍️ 提示词编写指南
基本结构
- 主体描述:要生成什么(人物、物体、场景)
- 风格定义:艺术风格、绘画技法
- 细节补充:颜色、光线、构图等
示例提示词
人物肖像
一位美丽的亚洲女性,长发飘逸,微笑,专业摄影,柔和光线,高清,细节丰富风景场景
日出时分的山脉景色,金色阳光,云海翻腾,宽阔视角,风景摄影,8K高清艺术作品
油画风格的向日葵花田,梵高风格,厚重笔触,明亮色彩,艺术masterpiece🖼️ 效果展示
输入示例
输入文字:一只可爱的橙色小猫坐在窗台上,阳光透过窗户洒在它身上,温暖的光线,高清摄影
输出结果
输出图片:

💡 使用技巧
最佳实践
- 描述清晰:提供清晰、具体的描述
- 风格一致:保持整体风格的一致性
- 细节丰富:添加颜色、光线、构图等细节描述
⚠️ 注意事项
- 内容审核:生成内容会经过安全审核
- 版权合规:请确保生成内容的合法使用
- 处理时间:根据提示词复杂度,处理时间在5-15秒之间
- URL有效期:返回的图像URL地址有效期为1个小时,请及时保存或下载