用途
Stability AI 技能通过其 REST API(v2beta / v1)提供最先进的图像生成、编辑与放大能力,用于在自动化流程或命令行环境中集成图像处理。官方文档地址:https://platform.stability.ai/docs/api-reference
主要能力
- 文生图:SD3、SDXL Ultra 和 Core 模型,根据文本提示生成图像。
- 图像放大:支持保守(conservative)或创意(creative)放大。
- 局部重绘(inpainting):填充图像中被蒙版标记的区域。
- 图像外扩(outpainting):将图像向边界外延伸。
- 草图转图像:将草图或线稿转换为成品图。
使用方式
需先连接 Stability AI 连接器(app.vm0.ai/connectors)。若请求失败,可运行 zero doctor check-connector --env-name STABILITY_TOKEN 或 zero doctor check-connector --url https://api.stability.ai/v1/user/account --method GET 进行排查。
注意:多数生成端点默认返回二进制图像数据,使用 --output /tmp/image.png 保存文件;如需 base64 JSON 响应,可添加请求头 Accept: application/json。
1. 检查账户与积分
curl -s -X GET "https://api.stability.ai/v1/user/account" --header "Authorization: Bearer $STABILITY_TOKEN"
2. 生成图像 — Core(推荐,最快)
将以下内容写入 /tmp/stability_request.json:
{
"prompt": "A photorealistic mountain landscape at golden hour, 4k, highly detailed",
"output_format": "png"
}
然后运行(保存图像到文件):
curl -s -X POST "https://api.stability.ai/v2beta/stable-image/generate/core" --header "Authorization: Bearer $STABILITY_TOKEN" --header "Accept: image/*" -F prompt="A photorealistic mountain landscape at golden hour, 4k, highly detailed" -F output_format="png" --output /tmp/generated_image.png
3. 生成图像 — Ultra(SDXL,最高质量)
curl -s -X POST "https://api.stability.ai/v2beta/stable-image/generate/ultra" --header "Authorization: Bearer $STABILITY_TOKEN" --header "Accept: image/*" -F prompt="A futuristic city skyline at night with neon lights, cinematic" -F output_format="png" --output /tmp/generated_ultra.png
4. 生成图像 — SD3(Stable Diffusion 3)
将以下内容写入 /tmp/sd3_gen.sh:
curl -s -X POST "https://api.stability.ai/v2beta/stable-image/generate/sd3" --header "Authorization: Bearer $STABILITY_TOKEN" --header "Accept: image/*" -F prompt="An abstract digital painting with vibrant colors and geometric shapes" -F model="sd3-large" -F output_format="png" --output /tmp/generated_sd3.png
可用 model 值:sd3-large、sd3-large-turbo、sd3-medium
5. 生成图像并获取 Base64 JSON 响应
将以下内容写入 /tmp/stability_b64.json。使用 Accept: application/json 接收 JSON 中 base64 编码的图像:
curl -s -X POST "https://api.stability.ai/v2beta/stable-image/generate/core" --header "Authorization: Bearer $STABILITY_TOKEN" --header "Accept: application/json" -F prompt="A serene Japanese zen garden with cherry blossoms" -F output_format="png" | jq -r '.image' | base64 -d > /tmp/generated_b64.png
6. 保守图像放大
放大已有图像(需提供本地文件路径):
curl -s -X POST "https://api.stability.ai/v2beta/stable-image/upscale/conservative" --header "Authorization: Bearer $STABILITY_TOKEN" --header "Accept: image/*" -F image="@/tmp/source_image.png" -F prompt="high quality, sharp details" -F output_format="png" --output /tmp/upscaled_image.png
7. 局部重绘(编辑图像区域)
局部重绘需要源图像和蒙版(白色 = 待填充区域,黑色 = 保留区域)。将以下内容写入 /tmp/inpaint_note.txt:提供 -F image="@/tmp/source.png" 和 -F mask="@/tmp/mask.png"。蒙版尺寸应与源图像相同。蒙版中白色像素将被替换,黑色像素保留。
curl -s -X POST "https://api.stability.ai/v2beta/stable-image/edit/inpaint" --header "Authorization: Bearer $STABILITY_TOKEN" --header "Accept: image/*" -F image="@/tmp/source.png" -F mask="@/tmp/mask.png" -F prompt="A beautiful garden with blooming flowers" -F output_format="png" --output /tmp/inpainted.png
8. 图像外扩(将图像扩展到边界之外)
通过指定各边要添加的像素量来扩展图像:
curl -s -X POST "https://api.stability.ai/v2beta/stable-image/edit/outpaint" --header "Authorization: Bearer $STABILITY_TOKEN" --header "Accept: image/*" -F image="@/tmp/source.png" -F left=200 -F right=200 -F prompt="Seamlessly extend the scene" -F output_format="png" --output /tmp/outpainted.png
9. 草图转图像
将草图或线稿转换为成品图:
curl -s -X POST "https://api.stability.ai/v2beta/stable-image/control/sketch" --header "Authorization: Bearer $STABILITY_TOKEN" --header "Accept: image/*" -F image="@/tmp/sketch.png" -F prompt="A detailed architectural drawing rendered as a photorealistic building" -F output_format="png" --output /tmp/sketch_result.png
适用场景
- 需要从文本提示批量生成图像。
- 对已有图像进行放大、区域编辑或边界扩展。
- 将草图或线稿转换为成品图像。
- 在自动化脚本或命令行工作流中集成图像生成与编辑。
使用准则与限制
- 图像格式:使用
-F output_format="png"获得无损质量;webp 或 jpeg 可减小文件体积。 - 提示词:应具有描述性,包含风格、光照、质量描述词,如 "photorealistic"、"4k"、"cinematic"。
- 负面提示词:可在任意生成端点添加
-F negative_prompt="blurry, low quality, distorted"以排除不需要的元素。 - 宽高比:可在 Core 和 Ultra 添加
-F aspect_ratio="16:9"(或 1:1、9:16、21:9、2:3、3:2、4:5、5:4)。 - 积分:每次生成消耗积分,可通过
/v1/user/account端点检查余额。 - 文件输出:始终使用
--output /tmp/filename.png保存二进制响应;不要将二进制输出通过管道传给 jq。 - SD3 turbo:使用 sd3-large-turbo 可获得更快(质量较低)的生成;turbo 模型请省略 negative_prompt。
- 需先连接 Stability AI 连接器。
- 收费、价格等具体信息暂未提供。