概览
适用于 Python 的背景移除 API
Python 非常适合自动化、批量处理、AI 工作流以及后端系统集成。
本指南使用 requests 库发送图片并接收透明 PNG 结果。
STEP 1
安装 Requests 库
在运行 Python API 示例之前,请先安装 requests 库。
pip install requests
STEP 2
Python API 请求示例
使用 Python 发送图片并获取透明 PNG 结果。
import requests
API_URL = (
"https://api.bybybg.com/v1/remove-bg"
)
API_KEY = "YOUR_API_KEY"
with open("image.jpg", "rb") as image:
response = requests.post(
API_URL,
headers={
"X-API-Key": API_KEY
},
files={
"file": image
}
)
if response.status_code == 200:
with open("no-bg.png", "wb") as f:
f.write(response.content)
print("Background removed")
else:
print(
response.status_code,
response.text
)
USE CASES
BEST PRACTICES
最佳实践
- 将 API 密钥存储在环境变量中。
- 使用超时和重试机制。
- 上传前验证图片文件。
- 正确处理 API 错误。
- 所有 API 连接均使用 HTTPS。
ERRORS
Status
Description
401
413
429
500
继续学习 Flutter
了解如何将 ByByBG API 集成到 Android 和 iOS Flutter 应用中。