Install dependencies
Install any libraries needed for HTTP requests.
pip install requestsA complete guide to integrating FAPIhub's background removal API into your Python project. Takes less than 10 minutes.
This guide covers everything you need to integrate background removal into your Python project using FAPIhub's REST API. You'll go from zero to processing your first image in under 10 minutes. No machine learning knowledge required — just standard HTTP requests.
Install any libraries needed for HTTP requests.
pip install requestsCreate a free account and copy your API key from the dashboard.
Upload an image and receive transparent PNG output.
import requests
response = requests.post(
"https://fapihub.com/v2/rembg/",
headers={"token": "YOUR_API_KEY"},
files={"image": open("photo.jpg", "rb")}
)
with open("result.png", "wb") as f:
f.write(response.content)Write API response bytes to a PNG file and verify output.
with open("result.png", "wb") as f:
f.write(response.content)
# Verify it's a valid PNG
from PIL import Image
img = Image.open("result.png")
print(f"Output: {img.size[0]}x{img.size[1]}px, mode: {img.mode}")
# Mode should be "RGBA" for transparent backgroundCheck HTTP status codes and catch network errors.
import requests
def remove_background(image_path, api_key):
try:
with open(image_path, "rb") as f:
response = requests.post(
"https://fapihub.com/v2/rembg/",
headers={"token": api_key},
files={"image": f},
timeout=30
)
if response.status_code == 200:
return response.content
elif response.status_code == 401:
raise ValueError("Invalid API key. Check your token.")
elif response.status_code == 429:
raise RuntimeError("Rate limit reached. Wait before retrying.")
else:
raise RuntimeError(f"API error: {response.status_code}")
except requests.exceptions.Timeout:
raise RuntimeError("Request timed out. Try again.")
except requests.exceptions.ConnectionError:
raise RuntimeError("Network error. Check your connection.")Loop through image paths for high-volume jobs.
from pathlib import Path
import requests
INPUT_FOLDER = Path("images")
OUTPUT_FOLDER = Path("results")
OUTPUT_FOLDER.mkdir(exist_ok=True)
for image_path in list(INPUT_FOLDER.glob("*.jpg")) + list(INPUT_FOLDER.glob("*.png")):
with open(image_path, "rb") as image_file:
response = requests.post(
"https://fapihub.com/v2/rembg/",
headers={"token": "YOUR_API_KEY"},
files={"image": image_file},
timeout=30
)
if response.status_code == 200:
(OUTPUT_FOLDER / f"{image_path.stem}_nobg.png").write_bytes(response.content)Blur background while preserving the subject.
response = requests.post(
"https://fapihub.com/v2/rembg/blur/",
headers={"token": "YOUR_API_KEY"},
files={"image": open("photo.jpg", "rb")},
data={"blur_radius": 15} # 1–50, default 10
)
with open("blurred_bg.png", "wb") as f:
f.write(response.content)Set a solid RGB background color.
response = requests.post(
"https://fapihub.com/v2/rembg/color/",
headers={"token": "YOUR_API_KEY"},
files={"image": open("photo.jpg", "rb")},
data={"background_color": "255,255,255"} # RGB format
)
with open("white_bg.png", "wb") as f:
f.write(response.content)Create a gradient background output.
response = requests.post(
"https://fapihub.com/v2/rembg/gradient/",
headers={"token": "YOUR_API_KEY"},
files={"image": open("photo.jpg", "rb")},
data={"top_color": "0,0,255", "bottom_color": "255,255,255"}
)
with open("gradient_bg.png", "wb") as f:
f.write(response.content)Generate a shadow effect output.
response = requests.post(
"https://fapihub.com/v2/rembg/shadow/",
headers={"token": "YOUR_API_KEY"},
files={"image": open("photo.jpg", "rb")},
data={"shadow_color": "0,0,0", "offset": 10, "blur": 15}
)
with open("shadow.png", "wb") as f:
f.write(response.content)Free tier includes 100 full-resolution requests/month. No credit card. Your API key is ready in 30 seconds.
Get Your Free API Key