免费文生成图(Text-to-Image)API —— StableDiffusionAPI.com、Hugging Face Inference API、Replicat

概览

本文将介绍六种主流且对开发者友好的免费文本生成图像(Text-to-Image)API —— StableDiffusionAPI.com、Hugging Face Inference API、Replicate、Craiyon、Segmind 及 DeepAI,并为每种服务提供可直接使用的 Python 调用示例,帮助你在项目中快速集成高质量 AI 绘图功能。


1. StableDiffusionAPI.com

StableDiffusionAPI.com 提供了基于 Stable Diffusion V3 的免费试用额度,可以通过简单的 RESTful 接口生成图像。下面示例演示如何使用 Python 通过 requests 调用 Text-to-Image 端点: citeturn5search0turn5search1

import requests
import json

url = "https://stablediffusionapi.com/api/v3/text2img"
payload = json.dumps({
    "key": "YOUR_API_KEY",
    "prompt": "a futuristic cityscape at sunset",
    "width": "512",
    "height": "512",
    "samples": "1",
    "num_inference_steps": "20",
    "guidance_scale": 7.5
})
headers = {
    "Content-Type": "application/json"
}

response = requests.post(url, headers=headers, data=payload)
print(response.json())
  • 说明
    • key:在控制台获取的 API Key;
    • prompt:文本描述;
    • width/height:分辨率,最大支持 1024×1024;
    • num_inference_steps:去噪步数,常用 20–50。 citeturn5search0

2. Hugging Face Inference API

Hugging Face 提供 永久免费 的 Serverless Inference API,支持多种任务,包括文本生成图像。以下示例展示两种调用方式。

2.1 使用 requests + PIL

import io
import requests
from PIL import Image

endpoint = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
headers = {"Authorization": f"Bearer YOUR_HF_TOKEN"}
payload = {"inputs": "Astronaut riding a horse"}

response = requests.post(endpoint, headers=headers, json=payload)
image = Image.open(io.BytesIO(response.content))
image.show()
  • 说明
    • YOUR_HF_TOKEN:在 Hugging Face 账户设置获取;
    • 该方法直接返回二进制图像数据,可使用 Pillow 显示或保存。 citeturn6search4

2.2 使用官方 Python 客户端 huggingface_hub

from huggingface_hub import InferenceApi

inference = InferenceApi(repo_id="stabilityai/stable-diffusion-2-1", token="YOUR_HF_TOKEN")
# 直接调用会返回 PIL.Image 对象
img = inference("a serene mountain landscape")
img.save("output.png")
  • 安装
    pip install huggingface_hub[inference]
    
  • 特点:自动推断任务类型,支持原始响应 (raw_response=True) 等高级用法。 citeturn11search3

3. Replicate

Replicate 提供按秒计费的免费试用额度,并且有简单易用的 Python 客户端。下面示例演示如何调用任意公开模型(以 Stable Diffusion 为例): citeturn10search1

# 安装 Replicate 客户端
# pip install replicate

import replicate

# 调用模型生成图像
output = replicate.run(
    "stability-ai/stable-diffusion-2",
    input={"prompt": "a majestic lion by the sea"}
)

# 保存结果
with open("lion.png", "wb") as f:
    f.write(output[0].read())
print("Image saved as lion.png")
  • 说明
    • 默认返回 FileOutput 对象,可通过 .read() 获取二进制数据;
    • 支持 URL 或本地文件作为输入,适用于 img2img 等多种模型。 citeturn10search1

4. Craiyon(原 DALL·E mini)

Craiyon 官方提供免费的 Integration API,同时社区维护了 Python SDK craiyon.py。以下示例展示如何使用该 SDK: citeturn0search10turn0search17

# 安装 SDK
# pip install craiyon.py

from craiyon import Craiyon

generator = Craiyon()  # 实例化
result = generator.generate("Photorealistic image of shrek eating earth")

# 打印并下载图片 URL
for idx, url in enumerate(result.images):
    print(f"Image {idx + 1}: {url}")
  • 特点
    • 无需 API Key,即可免费调用;
    • 支持异步版本 .async_generate().async_save_images()。 citeturn0search10

5. Segmind

Segmind 提供对个人用户完全免费的 Stable Diffusion 2.1 Serverless API,并且有官方 Python 客户端 segmind-py。示例: citeturn0search11

# 安装 Segmind 客户端
# pip install segmind

from segmind import SD2_1

# 用你的 API Key 实例化模型
model = SD2_1(api_key="YOUR_SEGMIND_KEY")

# 生成图像
img = model.generate(prompt="a futuristic robot garden")

# 保存图像
with open("robot_garden.png", "wb") as f:
    f.write(img.read())
print("Image saved as robot_garden.png")
  • 优势
    • 支持多种细分模型(SD1.5、Kadinsky、Codeformer 等);
    • 可选参数丰富,易于集成。 citeturn0search11

6. DeepAI

DeepAI 提供文本生成图像的免费 API,基于 Stable Diffusion。调用方式如下: citeturn0search12

import requests

url = "https://api.deepai.org/api/text2img"
headers = {"api-key": "YOUR_DEEPAI_KEY"}
data = {"text": "shiba inu with sunglasses"}

response = requests.post(url, data=data, headers=headers)
print(response.json())
  • 响应:包含生成图像的 URL 列表,易于二次处理。 citeturn0search12

以上示例涵盖了目前主流的免费或免费层文本生成图像 API 的 Python 调用方法,涵盖从简单的 RESTful 请求到官方 SDK 的多种形式,可根据项目需求灵活选择与组合。祝你在开发 AI 绘图功能时事半功倍!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值