Skip to content

图像生成

一、接口请求体(Body)参数详解

1. prompt(必需)

  • 类型:字符串
  • 描述:用于生成图片的提示文本,描述希望生成的图像内容。
  • 示例值"an island near sea, with seagulls, moon shining over the sea, light house, boats in the background, fish flying over the sea"

2. guidance_scale(必需)

  • 类型:数字
  • 描述:控制生成图片与提示文本的匹配程度。值越高,生成图片越严格匹配文本提示;值越低,生成图片越具创意和多样性,可能包含更多意外元素。
  • 取值范围0 <= guidance_scale <= 20
  • 默认值7.5
  • 示例值7.5

二、使用示例

1. 基础图片生成请求示例

import requests
import json

# 设置接口URL
url = "https://api.siliconflow.cn/v1/images/generations"

# 设置请求头,包含认证信息和内容类型
headers = {
    "Authorization": "Bearer YOUR_API_KEY",  # 替换为你的API密钥
    "Content-Type": "application/json"       # 指定发送的数据格式为JSON
}

# 设置请求体
data = {
    "prompt": "an island near sea, with seagulls, moon shining over the sea, light house, boats in the background, fish flying over the sea",
    "guidance_scale": 7.5
}

# 发送POST请求
response = requests.post(url, headers=headers, json=data)

# 打印返回结果
print(response.json())

2. 完整图片生成请求示例(包含更多参数)

import requests
import json

url = "https://api.siliconflow.cn/v1/images/generations"

headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

# 完整请求体,包含所有参数
data = {
    "prompt": "an island near sea, with seagulls, moon shining over the sea, light house, boats in the background, fish flying over the sea",
    "guidance_scale": 7.5
}

response = requests.post(url, headers=headers, json=data)

print(response.json())