AIGC 文生图 API 完整教程
掌握 FLUX、Stable Diffusion、DALL-E 等主流 AI 绘画模型的 API 调用,创作惊艳的 AI 艺术作品
支持的图像生成模型
FLUX 1.1 Pro
flux-1.1-pro~10秒
最新的 FLUX 旗舰模型,极致的图像质量
最高 2048x2048
超高质量精准提示词理解复杂场景艺术创作
FLUX Dev
flux-dev~5秒
开发版本,平衡质量与速度
最高 1024x1024
高质量LoRA 支持快速迭代风格化
FLUX Schnell
flux-schnell~2秒
超快速生成,适合实时应用
最高 1024x1024
极速生成实时预览低成本批量处理
Stable Diffusion XL
stable-diffusion-xl~5秒
经典开源模型,社区生态丰富
最高 1024x1024
开源模型ControlNet大量 LoRA自定义训练
DALL-E 3
dall-e-3~15秒
OpenAI 的文生图模型,理解能力强
1024x1024, 1792x1024
自然语言理解文字渲染创意生成安全过滤
Midjourney
midjourney-v6~30秒
艺术化程度最高的模型
多种比例
艺术风格美学优化参数丰富社区驱动
代码示例
基础文生图
使用 FLUX 生成高质量图像
import requests
import json
# API 配置
API_KEY = "YOUR_API_KEY"
API_URL = "https://api.example.com/v1/images/generations"
# 请求参数
data = {
"model": "flux-1.1-pro", # 可选: flux-dev, flux-schnell
"prompt": "一只可爱的卡通猫咪坐在彩虹上,梦幻风格,8K画质,精细绘画",
"negative_prompt": "低质量,模糊,变形",
"width": 1024,
"height": 1024,
"num_inference_steps": 50, # 推理步数,越高质量越好
"guidance_scale": 7.5, # 提示词引导强度
"seed": 42 # 随机种子,相同种子生成相同图像
}
# 发送请求
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(API_URL, headers=headers, json=data)
result = response.json()
# 获取图像URL
image_url = result["data"][0]["url"]
print(f"生成的图像: {image_url}")
# 下载图像
import urllib.request
urllib.request.urlretrieve(image_url, "generated_image.png")参数说明
| 参数名 | 类型 | 说明 | 示例 |
|---|---|---|---|
prompt | string | 图像描述提示词,越详细效果越好 | a beautiful sunset over mountains, oil painting style |
negative_prompt | string | 负面提示词,描述不想出现的内容 | low quality, blurry, distorted |
width / height | integer | 图像尺寸,需要是 8 的倍数 | 1024 |
num_inference_steps | integer | 推理步数,越高质量越好但速度越慢 | 50 |
guidance_scale | float | 提示词引导强度,控制与提示词的匹配程度 | 7.5 |
seed | integer | 随机种子,相同种子生成相同图像 | 42 |
使用技巧
提示词技巧
- 使用具体的描述词:材质、光线、风格、颜色
- 添加质量词:masterpiece, best quality, ultra-detailed
- 指定艺术风格:oil painting, anime style, photorealistic
- 使用权重语法:(important:1.2), [less important:0.8]
常用风格关键词
- 写实风格:photorealistic, hyperrealistic, professional photography
- 艺术风格:oil painting, watercolor, digital art, concept art
- 动漫风格:anime, manga, cel shading, studio ghibli style
- 特殊效果:volumetric lighting, ray tracing, depth of field
性能优化
- 批量请求使用异步处理
- 根据用途选择合适的模型和参数
- 使用较低的推理步数进行快速预览
- 缓存常用的生成结果
常见问题解决
- 图像变形:检查宽高比,使用标准尺寸
- 质量不佳:增加推理步数,优化提示词
- 生成失败:检查提示词是否包含敏感内容
- 响应超时:减少批量请求数量,使用异步处理
提示词示例库
写实人像:
portrait of a young woman, natural lighting, shallow depth of field, 85mm lens, professional photography, soft skin, detailed eyes, warm color grading概念艺术:
futuristic cityscape, cyberpunk aesthetic, neon lights, flying vehicles, rain, reflections, volumetric fog, cinematic composition, concept art by simon stalenhag动漫风格:
anime girl, cherry blossoms, school uniform, flowing hair, cel shading, studio ghibli style, soft pastel colors, dreamy atmosphere, detailed background