DEV Community

Om Prakash
Om Prakash

Posted on

Remove Image Backgrounds in Python with 3 Lines of Code

Remove Image Backgrounds in Python with 3 Lines of Code

I built PixelAPI because I was tired of wrestling with model weights, CUDA drivers, and Docker containers just to remove a background from an image. Here's how you can do it now in literally 3 lines of Python.

The 3-Line Version

import pixelapi

client = pixelapi.Client("YOUR_API_KEY")
result = client.remove_background("photo.jpg")
Enter fullscreen mode Exit fullscreen mode

That's it. result is a transparent PNG saved to disk. No ML frameworks, no GPU, no 4GB model downloads.

How It Works Under the Hood

PixelAPI runs BiRefNet (Bilateral Reference Network) on dedicated GPUs. It's currently the state-of-the-art for image matting — significantly better than older U²-Net based approaches.

When you call remove_background():

  1. Your image is uploaded to the API
  2. BiRefNet processes it on a GPU (~2 seconds)
  3. You get back a transparent PNG

The Full Version (with requests)

If you prefer not using the SDK, here's the raw API version:

import requests, time

API_KEY = "YOUR_API_KEY"
BASE = "https://api.pixelapi.dev"

# Submit background removal job (multipart upload)
with open("photo.jpg", "rb") as f:
    resp = requests.post(
        f"{BASE}/v1/image/remove-background",
        headers={"Authorization": f"Bearer {API_KEY}"},
        files={"image": ("photo.jpg", f, "image/jpeg")}
    )
job_id = resp.json()["id"]

# Poll for result
while True:
    time.sleep(2)
    result = requests.get(
        f"{BASE}/v1/image/{job_id}",
        headers={"Authorization": f"Bearer {API_KEY}"}
    ).json()
    if result["status"] == "completed":
        img_data = requests.get(result["output_url"]).content
        with open("output.png", "wb") as f:
            f.write(img_data)
        print("Done! Saved to output.png")
        break
    elif result["status"] in ("failed", "blocked"):
        print(f"Error: {result.get('error_message', result['status'])}")
        break
Enter fullscreen mode Exit fullscreen mode

When Would You Use This?

Some real use cases I've seen from PixelAPI users:

  • E-commerce: Batch-process product photos for clean white/transparent backgrounds
  • Design tools: Let users remove backgrounds in your SaaS app
  • Social media: Automated thumbnail generation
  • Real estate: Isolate furniture/objects from room photos

Comparison: Running BiRefNet Locally vs API

Aspect Local (BiRefNet) PixelAPI
Setup time 30-60 min (CUDA, PyTorch, model weights) 2 min (pip install + API key)
GPU required Yes (4GB+ VRAM) No
Model size ~1.5GB download 0
Processing speed ~2s/image ~2s/image
Cost Free (your hardware) 2 credits/image (~$0.002)
Maintenance You manage everything Managed

If you're processing millions of images and have GPU infrastructure, run it locally. For everything else, an API call is simpler.

Getting Started

  1. Sign up at pixelapi.dev (Google sign-in, no credit card)
  2. You get 100 free credits immediately
  3. Install the SDK: pip install pixelapi
  4. That's it

The background removal endpoint costs 2 credits per image. So your free tier gives you 50 background removals to start.

Try It Without Signing Up

I also built a free web tool where you can test background removal directly in your browser — no account needed.


I'm Om, the solo founder behind PixelAPI. I'm building this from Hyderabad, India. If you have questions or feedback, hit me up at support@pixelapi.dev.

Top comments (0)