Jasper 承接后的 Image API:
POST https://api.jasper.ai/v1/image/remove-background,头X-API-Key。适合已有 Jasper Business + Image 订阅的团队。
先看清成品
- mock 返回 PNG cutout。
- 1 项 pytest 通过。
合规提醒:Clipdrop 现属 Jasper;网页工具仍可用,API 额度/新模型以 Jasper 沟通与文档为准。只处理你有权使用的图片;勿去除水印用于侵权或伪造。输入限制常见为 30MB / 25MP(以文档为准)。本文不编造单价。
运行说明:无 Clipdrop/Jasper Image 正式密钥。A/C 用 mock 去背;对官方 clipdrop-api.co 无效密钥实测 HTTP 403。B 为本地体积预检。2026-10-23(Asia/Shanghai)pytest 通过。
做完之后你会得到什么?
| 成果 | 说明 |
|---|---|
jasper_remove_bg.py |
Jasper 客户端 |
mock_jasper_image.py |
假服务 |
sample.png |
样例 |
test_109c.py |
测试 |
需要准备哪些环境?
| 项目 | 说明 |
|---|---|
| Python | 3.10+ |
| 环境变量 | JASPER_API_KEY |
关键事实
| 事实 | 来源 |
|---|---|
| Image API 需 Business + API 订阅(以官网为准) | 官方 Remove background |
| 认证可用 workspace API key(X-API-Key) | 官方 Authentication |
| 与 Clipdrop 主机不同,密钥体系也可能不同 | 重要 |
文件结构
109-clipdrop-C-src/
├── jasper_remove_bg.py
├── mock_jasper_image.py
├── sample.png
└── test_109c.py
第 1 步:客户端
#!/usr/bin/env python3
"""Jasper Image API remove-background client (successor path for Clipdrop).
POST https://api.jasper.ai/v1/image/remove-background
Header: X-API-Key Field: image_file
"""
from __future__ import annotations
import argparse, os, sys, urllib.error, urllib.request
def remove_bg(base, key, image_path, out_path):
with open(image_path, "rb") as f:
content = f.read()
filename = image_path.split("/")[-1]
boundary = "----binbhoJasper"
body = (
f"--{boundary}\r\nContent-Disposition: form-data; name=\"image_file\"; filename=\"{filename}\"\r\n"
f"Content-Type: application/octet-stream\r\n\r\n"
).encode() + content + f"\r\n--{boundary}--\r\n".encode()
req = urllib.request.Request(
f"{base.rstrip('/')}/v1/image/remove-background",
data=body,
headers={"X-API-Key": key, "Content-Type": f"multipart/form-data; boundary={boundary}", "Accept": "image/png"},
method="POST",
)
try:
with urllib.request.urlopen(req, timeout=120) as resp:
data = resp.read()
with open(out_path, "wb") as f:
f.write(data)
return {"bytes": len(data), "out": out_path}
except urllib.error.HTTPError as e:
raise SystemExit(f"HTTP {e.code}: {e.read().decode(errors='replace')}") from e
def main(argv=None) -> int:
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("image")
ap.add_argument("-o", "--out", default="cutout.png")
ap.add_argument("--base", default=os.environ.get("JASPER_BASE_URL", "https://api.jasper.ai"))
ap.add_argument("--key", default=os.environ.get("JASPER_API_KEY", ""))
args = ap.parse_args(argv)
if not args.key:
print("设置 JASPER_API_KEY(Business + Image API 订阅,以官网为准)", file=sys.stderr); return 2
print(remove_bg(args.base, args.key, args.image, args.out))
return 0
if __name__ == "__main__":
raise SystemExit(main())
第 2 步:Mock
#!/usr/bin/env python3
"""Mock Jasper Image remove-background."""
from __future__ import annotations
from http.server import BaseHTTPRequestHandler, HTTPServer
API_KEY = "jasper-test-key"
PNG = (
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
b"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\nIDATx\x9cc\x00\x01"
b"\x00\x00\x05\x00\x01\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82"
)
class H(BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def do_POST(self):
if self.path != "/v1/image/remove-background":
self.send_error(404); return
if self.headers.get("X-API-Key") != API_KEY:
b = b'{"error":"unauthorized"}'
self.send_response(401); self.send_header("Content-Type","application/json"); self.send_header("Content-Length",str(len(b))); self.end_headers(); self.wfile.write(b); return
n = int(self.headers.get("Content-Length", 0)); self.rfile.read(n)
self.send_response(200); self.send_header("Content-Type","image/png"); self.send_header("Content-Length",str(len(PNG))); self.end_headers(); self.wfile.write(PNG)
def main():
import argparse
ap = argparse.ArgumentParser(); ap.add_argument("--port", type=int, default=9110)
args = ap.parse_args()
HTTPServer(("127.0.0.1", args.port), H).serve_forever()
if __name__ == "__main__":
main()
第 3 步:测试
import subprocess, sys, time
from pathlib import Path
HERE = Path(__file__).resolve().parent
def test_jasper():
port = 19191
proc = subprocess.Popen([sys.executable, str(HERE/"mock_jasper_image.py"), "--port", str(port)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
time.sleep(0.3)
out = HERE / "_j.png"
subprocess.run([sys.executable, str(HERE/"jasper_remove_bg.py"), str(HERE/"sample.png"), "-o", str(out), "--base", f"http://127.0.0.1:{port}", "--key", "jasper-test-key"], check=True, capture_output=True)
assert out.exists()
finally:
proc.terminate(); proc.wait(timeout=5)
if (HERE/"_j.png").exists():
(HERE/"_j.png").unlink()
第 4 步:运行
$ pytest jasper remove_bg
. [100%]
1 passed in 0.36s
怎么检查结果?
- 输出 PNG 非空。
- 确认套餐含 Image API 再切流量。
调试示例
Clipdrop 密钥拿来调 api.jasper.ai。 通常不行。在 Jasper Settings → API Tokens 另建。
常见问题
| 现象 | 原因 | 处理 |
|---|---|---|
| 哪条链路 | 旧 clipdrop-api 或 Jasper | 看合同与额度 |
| OAuth | 用户代操作 | 服务端批处理用 API key |
| 其他编辑 | uncrop/upscale 等 | 查 Jasper Image 文档 |
模板规则
- 按合同选主机。
- 密钥分环境。
- 输出仍需人工质检。
项目资源
| 用途 | 地址 |
|---|---|
| Jasper Remove background(官方) | https://developers.jasper.ai/reference/removebackground |
| Jasper Authentication(官方) | https://developers.jasper.ai/docs/authentication |
| Jasper Image | https://images.jasper.ai/ |
Clipdrop 能用,Jasper Image 才是新业务的主路。