您现在的位置是:网站首页> AI人工智能
Stable Diffusion 技术收集
- AI人工智能
- 2025-11-07
- 27人已阅读
Stable Diffusion 技术收集

stable-diffusion API文档:开发人员参考指南
stable-diffusion API文档:开发人员参考指南
####【豆包】
Q:知道开源的Stable Diffusion吗
A:...
Q:Stable Diffusion API开发文档
A:..
Stable Diffusion 的 API 开发可通过官方服务、第三方平台或本地部署实现。
AUTOMATIC1111 API 参考:https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API
Q:本地部署的Stable Diffusion如何通过API调用绘图
A:...
一、启用 WebUI 的 API 功能
Stable Diffusion WebUI 内置了 API 支持,但默认未开启,需通过启动参数启用:
修改启动脚本
找到 WebUI 的启动脚本(Windows 为webui-user.bat,Linux 为webui-user.sh),右键编辑。
在启动参数中添加 --api,示例:
bash
# Windows (webui-user.bat)
set COMMANDLINE_ARGS=--api --xformers --enable-insecure-extension-access
# Linux (webui-user.sh)
export COMMANDLINE_ARGS="--api --xformers"
其他可选参数:--listen(允许局域网访问,地址为http://本机IP:7860)、--port 8000(修改端口,避免冲突)。
启动 WebUI运行脚本,启动成功后,API 接口会随 WebUI 一起启动,默认地址为 http://127.0.0.1:7860/api。
二、API 接口说明与调用示例
WebUI 的 API 基于 FastAPI 实现,支持文本生成图像(txt2img)、图像生成图像(img2img)等核心功能,完整接口文档可访问 http://127.0.0.1:7860/docs 查看。
1. 核心接口:文本生成图像(txt2img)
接口地址:http://127.0.0.1:7860/sdapi/v1/txt2img请求方法:POST请求体参数(JSON 格式):
参数名 含义说明 示例值
prompt 正向提示词(描述生成内容) "a cat wearing a hat, oil painting style"
negative_prompt 反向提示词(排除不想要的内容) "blurry, low quality, text"
steps 采样步数(越大越精细,通常 20-50) 30
width/height 生成图像宽高(需符合模型训练尺寸,如 512x512、768x768) 512, 512
sampler_index 采样器(如 "Euler a"、"DPM++ 2M Karras") "Euler a"
cfg_scale 提示词相关性(越大越严格遵循提示词,通常 7-12) 7.5
batch_size 一次生成的图像数量 1
2. Python 调用示例(txt2img)
使用requests库发送 POST 请求,获取生成的图像并保存:
python
运行
import requests
import base64
import json
from PIL import Image
from io import BytesIO
# API地址
url = "http://127.0.0.1:7860/sdapi/v1/txt2img"
# 请求参数
payload = {
"prompt": "a cyberpunk city at night, neon lights, rain, 8k resolution",
"negative_prompt": "ugly, distorted, lowres, watermark",
"steps": 30,
"width": 768,
"height": 512,
"sampler_index": "DPM++ 2M Karras",
"cfg_scale": 8,
"batch_size": 1
}
# 发送请求
response = requests.post(url=url, json=payload)
result = response.json()
# 解析返回结果(图像以base64编码存储在images字段)
for i, img_data in enumerate(result["images"]):
# 解码base64为图像
image = Image.open(BytesIO(base64.b64decode(img_data)))
# 保存图像
image.save(f"generated_image_{i}.png")
print(f"图像已保存:generated_image_{i}.png")
3. 图像生成图像(img2img)调用示例
若需基于已有图像生成新内容(如风格迁移、局部重绘),使用/sdapi/v1/img2img接口,核心参数需增加原始图像的 base64 编码:
python
运行
import requests
import base64
from PIL import Image
from io import BytesIO
# 读取本地图像并转为base64
def image_to_base64(image_path):
with Image.open(image_path) as img:
buffered = BytesIO()
img.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode()
# 原始图像路径
img_path = "input_image.png"
img_base64 = image_to_base64(img_path)
# API地址
url = "http://127.0.0.1:7860/sdapi/v1/img2img"
# 请求参数(新增init_images字段)
payload = {
"init_images": [img_base64], # 原始图像的base64列表
"prompt": "turn the photo into a Van Gogh style painting",
"negative_prompt": "photorealistic, modern elements",
"steps": 30,
"denoising_strength": 0.75, # 去噪强度(0-1,越大越偏离原图)
"sampler_index": "Euler a"
}
# 发送请求并保存结果
response = requests.post(url=url, json=payload)
result = response.json()
for i, img_data in enumerate(result["images"]):
image = Image.open(BytesIO(base64.b64decode(img_data)))
image.save(f"img2img_result_{i}.png")
上一篇:AI人工智能应用编程