DEV Community

Cover image for Remove Background from Clothing Photos with an API (Python)
AI Engine
AI Engine

Posted on Originally published at ai-engine.net

Remove Background from Clothing Photos with an API (Python)

Wardrobe apps, resale marketplaces and styling tools share one problem: users photograph their clothes on a bed, a door or a kitchen table, and the result looks nothing like a catalog. A background removal step fixes that. You send the photo, you get back the garment alone on a transparent background, and you can put it on a clean tile or combine several items into an outfit.

This tutorial builds that pipeline in Python: remove the background, make square closet tiles, name the colors, batch import user uploads and build an outfit board. Every script was run on real photos of clothes taken at home.

Want to try it on your own clothes? Get a key for the Background Removal API.


What the API returned on four clothing photos

We picked photos that look like real user uploads:

  • Cream blouse on a white door: light fabric on a light background, with hinges and a hanger hook in the frame.
  • Denim jacket on a wooden table: the jacket fills almost the whole frame.
  • Black t-shirt on a hanger: strong window shadows on the wall behind it.
  • Black sneakers on carpet: loose white laces spreading away from the shoes.

All four came back as clean cutouts in one request each. The blouse kept its edges against the white door, the shadows disappeared from behind the t-shirt, and the loose laces were kept. Hangers stay attached to the garment, the usual choice for a closet view.

Four phone photos of clothes above the same items as clean square tiles with color tags


Remove the background in Python

You need Python 3 with requests and pillow, and a RapidAPI key in RAPIDAPI_KEY. Save this as closet_api.py:

import io
import os
import requests
from PIL import Image

API_URL = "https://background-removal-ai.p.rapidapi.com/remove-background"
HEADERS = {
    "x-rapidapi-host": "background-removal-ai.p.rapidapi.com",
    "x-rapidapi-key": os.environ["RAPIDAPI_KEY"],
}
MAX_SIDE = 1500  # phone photos are 4000 px wide; the closet does not need that


def remove_background(path):
    """Upload a clothing photo and return the cutout as an RGBA image."""
    img = Image.open(path).convert("RGB")
    img.thumbnail((MAX_SIDE, MAX_SIDE))
    buf = io.BytesIO()
    img.save(buf, "JPEG", quality=92)
    buf.seek(0)

    r = requests.post(API_URL, headers=HEADERS,
                      files={"image": ("item.jpg", buf, "image/jpeg")}, timeout=60)
    r.raise_for_status()
    png_url = r.json()["image_url"]
    png = requests.get(png_url, timeout=60).content
    return Image.open(io.BytesIO(png)).convert("RGBA")


if __name__ == "__main__":
    cutout = remove_background("jacket.jpg")
    cutout.save("jacket.png")
    print(cutout.size, cutout.getbbox())
Enter fullscreen mode Exit fullscreen mode
(1500, 1339) (28, 0, 1500, 1339)
Enter fullscreen mode Exit fullscreen mode

The API returns JSON with image_url (a link to the transparent PNG), width, height and size_bytes. If your photos are already online, send json={"image_url": url} instead of the file.


Square closet tiles and color tags

The transparent PNG makes two things easy. getbbox() gives the box around the garment, so you can crop and center it on a square. And since the background is gone, counting colors gives the color of the item, not of the door behind it. Save as closet_tiles.py:

import colorsys
from collections import Counter
from PIL import Image


def make_tile(cutout, size=1000, fill=0.86, bg=(244, 244, 245)):
    """Crop to the garment and center it on a square tile with a neutral background."""
    item = cutout.crop(cutout.getbbox())
    scale = size * fill / max(item.size)
    item = item.resize((round(item.width * scale), round(item.height * scale)),
                       Image.LANCZOS)
    tile = Image.new("RGBA", (size, size), bg + (255,))
    tile.alpha_composite(item, ((size - item.width) // 2, (size - item.height) // 2))
    return tile.convert("RGB")


def color_name(r, g, b):
    h, s, v = colorsys.rgb_to_hsv(r / 255, g / 255, b / 255)
    h *= 360
    if v < 0.25:
        return "black"
    if s < 0.12:
        return "white" if v > 0.82 else "gray"
    if s < 0.30 and 20 <= h < 70:
        return "cream" if v > 0.7 else "beige"
    if h < 15 or h >= 345:
        return "red"
    if h < 40:
        return "brown" if v < 0.6 else "orange"
    if h < 70:
        return "yellow"
    if h < 170:
        return "green"
    if h < 255:
        return "navy" if v < 0.45 else ("light blue" if s < 0.45 else "blue")
    if h < 290:
        return "purple"
    return "pink"


def color_tags(cutout, top=2, min_share=8):
    """Name the main colors of the garment, ignoring transparent pixels."""
    small = cutout.copy()
    small.thumbnail((200, 200))
    pixels = [p[:3] for p in small.getdata() if p[3] > 200]
    counts = Counter(color_name(*p) for p in pixels)
    tags = [(name, round(100 * n / len(pixels))) for name, n in counts.most_common(top)]
    return [t for t in tags if t[1] >= min_share]  # drop hangers, labels, buttons


if __name__ == "__main__":
    from closet_api import remove_background
    for name in ["blouse", "jacket", "tshirt", "sneakers"]:
        cutout = remove_background(f"{name}.jpg")
        make_tile(cutout).save(f"{name}_tile.jpg", quality=90)
        print(f"{name:<9}", color_tags(cutout))
Enter fullscreen mode Exit fullscreen mode
blouse    [('beige', 73), ('brown', 14)]
jacket    [('light blue', 92)]
tshirt    [('black', 97)]
sneakers  [('black', 37), ('beige', 21)]
Enter fullscreen mode Exit fullscreen mode

HSV beats raw RGB because a shadow on a light blue jacket is still light blue in hue. The min_share filter drops small objects: without it, the wooden hanger added "orange" to the black t-shirt. Tags describe the color as photographed, so under warm bulbs a cream blouse reads as beige; let users fix a tag in one tap.


Batch import a closet

New users add many items at once. Run them in parallel, retry network errors, and keep two files per item: the PNG for outfit boards and the tile for the grid.

import json
import time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path

import requests
from closet_api import remove_background
from closet_tiles import make_tile, color_tags

OUT = Path("closet")
OUT.mkdir(exist_ok=True)


def import_item(path, retries=3):
    for attempt in range(retries):
        try:
            cutout = remove_background(path)
            break
        except requests.RequestException as e:
            if attempt == retries - 1:
                return {"file": path.name, "error": str(e)}
            time.sleep(2 ** attempt)

    item_id = path.stem
    cutout.save(OUT / f"{item_id}.png")              # transparent, for outfit boards
    make_tile(cutout).save(OUT / f"{item_id}_tile.jpg", quality=88)  # for the grid
    return {"id": item_id, "colors": [c for c, _ in color_tags(cutout)],
            "png": f"{item_id}.png", "tile": f"{item_id}_tile.jpg"}


photos = sorted(Path("uploads").glob("*.jpg"))
start = time.time()
with ThreadPoolExecutor(max_workers=4) as pool:
    catalog = list(pool.map(import_item, photos))

(OUT / "catalog.json").write_text(json.dumps(catalog, indent=2))
print(f"{len(catalog)} items in {time.time() - start:.1f} s")
for item in catalog:
    print(item.get("id"), item.get("colors", item.get("error")))
Enter fullscreen mode Exit fullscreen mode
4 items in 6.3 s
blouse ['beige', 'brown']
jacket ['light blue']
sneakers ['black', 'beige']
tshirt ['black']
Enter fullscreen mode Exit fullscreen mode

Build an outfit board

Transparent PNGs combine freely. This function fits each cutout in a box and adds a soft shadow:

from PIL import Image, ImageFilter


def place(board, png_path, box):
    """Fit a transparent cutout inside box (x, y, w, h) with a soft shadow."""
    item = Image.open(png_path).convert("RGBA")
    item = item.crop(item.getbbox())
    x, y, w, h = box
    scale = min(w / item.width, h / item.height)
    item = item.resize((round(item.width * scale), round(item.height * scale)),
                       Image.LANCZOS)
    ox, oy = x + (w - item.width) // 2, y + (h - item.height) // 2

    pad = 40  # room for the blur, so the shadow has no hard edges
    shadow = Image.new("RGBA", (item.width + 2 * pad, item.height + 2 * pad), (0, 0, 0, 0))
    mask = Image.new("L", shadow.size, 0)
    mask.paste(item.getchannel("A").point(lambda a: a * 0.25), (pad, pad))
    shadow.putalpha(mask.filter(ImageFilter.GaussianBlur(12)))
    board.alpha_composite(shadow, (ox - pad + 10, oy - pad + 14))
    board.alpha_composite(item, (ox, oy))


board = Image.new("RGBA", (1200, 750), (250, 247, 242, 255))
place(board, "closet/jacket.png", (60, 60, 520, 470))
place(board, "closet/tshirt.png", (620, 40, 520, 420))
place(board, "closet/sneakers.png", (380, 470, 440, 250))
board.convert("RGB").save("outfit_board.jpg", quality=90)
Enter fullscreen mode Exit fullscreen mode

Outfit board with the denim jacket, the black t-shirt on its hanger and the sneakers, each with a soft shadow

The shadow is padded before blurring; otherwise items that touch the edge of their photo, like the jacket, get a shadow with a hard rectangular border.


Production tips

  • Resize before upload. 1500 px on the longest side is plenty for tiles and boards.
  • Guide the photo, not the edit. A hint like "one item, whole garment in frame" in the camera screen helps most. The API handles the messy room.
  • Keep the transparent PNG as the master file. Tiles and boards can be regenerated without a second API call.
  • Process in a background job. Show the original right away, then swap in the tile when it is ready.
  • Reuse it for resale listings. See background removal at scale for e-commerce for white backgrounds and catalog rules.

Full tutorial with images: Remove Background from Clothing Photos on ai-engine.net.


Sources

Try it on your own clothes: grab a key from the Background Removal API page and run closet_api.py.

Top comments (0)