readme目前是ai写的( 如果有问题的话 记得联系我改(
基于 FastAPI + Pillow 实现的sky祈福签图片生成 API
- Python: 3.12+
- FastAPI: Web 框架
- Pillow: 图像处理
- Uvicorn: ASGI 服务器
- TOML: 配置文件解析
# 创建虚拟环境
python -m venv venv # Windows
python3 -m venv venv # Linux/macOS
# 激活虚拟环境 并打印一下解释器路径 确保虚拟环境激活
.\venv\Scripts\activate # Windows
Get-Command python # Windows PowerShell
source venv/bin/activate # Linux/macOS
which python # Linux bash
# 安装依赖
# pip install fastapi uvicorn pillow python-multipart toml
pip install -r ./requirements.txt编辑 config.toml:
[server]
host = "0.0.0.0"
port = 51205
log_level = "debug" # "info" 或 "debug"
[image]
width = 1240
height = 620
font_size = 40
assets_dir = "../assets"cd src
python main.py或使用 uvicorn 直接运行:
cd src
uvicorn main:app --host 0.0.0.0 --port 51205
# --reload可选
uvicorn main:app --host 0.0.0.0 --port 51205 --reload- 主页: http://localhost:51205/
- 获取祈福签: http://localhost:51205/blessing
- API 文档: http://localhost:51205/docs
skyblessings-fastapi-pillow/
├── assets/ # 资源文件
│ ├── font/ # 字体文件
│ │ └── LXGWWenKaiMono-Medium.ttf
│ └── image/ # 图片资源
│ ├── background.png # 遮罩图
│ ├── background0-5.png # 装饰背景
│ └── text0-4.png # 签文图片
├── src/ # 源代码
│ ├── main.py # FastAPI 主应用
│ ├── render.py # 图片渲染逻辑
│ └── draw_data.py # 祝福数据
├── venv/ # Python 虚拟环境
├── config.toml # 配置文件
└── README.md # 说明文档
返回 API 信息
响应示例:
{
"name": "祈福签 API",
"version": "1.0.0",
"endpoints": {
"/": "API 信息",
"/blessing": "获取随机祈福签图片(PNG)"
}
}生成并返回随机祈福签图片
响应类型: image/png
调试输出(log_level=debug 时):
--- 抽签结果 (Debug) ---
背景图: background1.png
签文图: text0.png
结缘物:飞鸟
缘彩:蔚蓝 (#28d1e9)
祝福语: 互相在意的人,不会走散。
忌:不思进取
--------------------------
host: 监听地址(默认0.0.0.0)port: 监听端口(默认51205)log_level: 日志级别(info或debug)
width: 图片宽度(默认1240)height: 图片高度(默认620)font_size: 字体大小(默认40)assets_dir: 资源文件夹路径
- 响应时间: 约 50-150ms
- 内存占用: 约 100-200MB
- 并发支持: FastAPI 异步处理
如果提示字体加载失败,检查:
assets/font/LXGWWenKaiMono-Medium.ttf文件是否存在config.toml中assets_dir路径是否正确
如果生成的图片颜色不对:
- 检查
assets/image/目录下所有 PNG 文件是否完整 - 查看日志中的错误信息
修改 config.toml 中的 port 值,或停止占用端口的进程:
# 查找占用端口的进程
netstat -ano | findstr :51205 # Windows PowerShell
sudo lsof -i :51205 # Linux bash
# 结束进程
taskkill /PID <进程ID> /F # Windows PowerShell
sudo kill -9 <进程ID> # Linux bash