
This week, we will discuss how to create an API that accepts an image and returns the image with the background removed.
Obviously, the image should be of a person or a model with a distinct background; otherwise, it won’t work properly.
You can check the free image background removal tool, and it works pretty well with most types of images.
Again, I will use ChatGPT to write the code for me. I wanted to use Python since it has free libraries for this purpose.
The Python script will run on a Windows server on a port using uvicorn. We will use IIS URL Rewrite to point to the directory where our script is located.
The Python script will provide an api endpoint where we receive the image, it will remove the background, and return the updated image to the user.
Since I am using this for my own website, I am only allowing my own website for CORS, so api endpoint is only available for my whitelisted domains.
Let’s start with ChatGPT and ask it to write the code for us.
Go to chatgpt.com and type “create a python app that removes background from the images”
Here is the response I got “Absolutely — you can build your own server-side API for background removal using open-source models like U^2-Net, MODNet, or MediaPipe. These run well using Python (with Flask or FastAPI) and libraries like OpenCV and PyTorch.”
🔧 What You’ll Get
-
A REST API (/remove-bg) that accepts an image file
-
Uses a deep learning model (U²-Net) to segment the image
-
Returns a PNG with transparent background
It uses https://github.com/danielgatis/rembg package, which wraps the U²-Net model for fast and good quality.
From here, if you know your way around Python, you can simply run the script using Python, but since I have never worked with Python, I need to know how to make it work with IIS.
So my next question was “lets do it step by step, first of all how do I install the server side script on a windows server”
I got the answer that I need to download and install Python on my Windows server. After the installation, open Command Prompt and create a virtual environment and activate venv.
Here is the command
mkdir background_removal_api
cd background_removal_api
python -m venv venv
venv\Scripts\activate
The next step is to install the required Python Packages.
The command for that is “pip install fastapi uvicorn python-multipart pillow opencv-python torch torchvision rembg”
Next, we need to create a main.py file that will have all the code to allow whitelisted domains, accept the image, and return the updated image.
Here is the code that I got after a few attempts.
from fastapi import FastAPI, File, UploadFile, Request, HTTPException
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.base import BaseHTTPMiddleware
from rembg import remove
from PIL import Image
import io
MAX_UPLOAD_BYTES = 10 * 1024 * 1024 # 20 MB
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=[
"https://thecanvasprints.co.uk",
"https://www.thecanvasprints.co.uk",
"https://bg.ikozmik.com",
],
allow_methods=["POST", "OPTIONS"], # include OPTIONS for preflight
allow_headers=["*"],
expose_headers=["Content-Disposition"], # optional, if you need it
allow_credentials=False, # set True only if you actually use cookies/auth
)
class LimitUploadSizeMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
length = request.headers.get("content-length")
if length is not None and int(length) > MAX_UPLOAD_BYTES:
raise HTTPException(status_code=413, detail="File too large (max 20 MB).")
return await call_next(request)
app.add_middleware(LimitUploadSizeMiddleware)
MAX_DIMENSION = 4000 # Max width or height in pixels
def resize_if_needed(image: Image.Image) -> Image.Image:
w, h = image.size
if max(w, h) <= MAX_DIMENSION:
return image # No resize needed
scale = MAX_DIMENSION / float(max(w, h))
new_size = (int(w * scale), int(h * scale))
return image.resize(new_size, Image.LANCZOS)
@app.post("/remove-bg")
async def remove_background(file: UploadFile = File(...)):
# Load image from uploaded bytes
image_bytes = await file.read()
input_image = Image.open(io.BytesIO(image_bytes)).convert("RGBA")
# Resize if needed
resized_image = resize_if_needed(input_image)
# Convert resized image to bytes
buf = io.BytesIO()
resized_image.save(buf, format="PNG")
resized_bytes = buf.getvalue()
# Run rembg on resized image
result = remove(resized_bytes)
return StreamingResponse(io.BytesIO(result), media_type="image/png")
I have implemented a few options such as image size allowed, whitelisted domains, etc.
You can see it's a simple, compact script that calls the rembg package and returns the image with the background removed.
There are a few other high-end scripts available as well. Some are paid and some are free, so we aim to use the free ones.
We can use this script to work with other free image background removal libraries.
Next, we need to save main.py file and run uvicorn command.
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
You can use any available port. This will start listening on port 8000.
Next, check if you can access Swagger at http://127.0.0.1:8000/docs
If you have followed all the instructions properly, this should work unless there is a firewall issue.
At this point, you should be able to access this localhost URL to convert the image. Next, you need to create a front end where the user uploads an image, and you call http://127.0.0.1:8000/remove-bg, which will remove the background and return the updated image.
Since I wanted to call the api from a different domain, I created a new subdomain and assigned it to the same directory the main.py file is in.
Next, I created this web.config file so it rewrites the subdomain URL to the api url. Here is the web.config file.
<configuration>
<system.webServer>
<security>
<requestFiltering>
<!-- 10 MB -->
<requestLimits maxAllowedContentLength="10485760" />
</requestFiltering>
</security>
<rewrite>
<rules>
<rule name="ProxyDocs" stopProcessing="true">
<match url="^docs(.*)" />
<action type="Rewrite" url="http://127.0.0.1:8000/docs{R:1}" />
</rule>
<rule name="ProxyOpenAPI" stopProcessing="true">
<match url="^openapi\.json$" />
<action type="Rewrite" url="http://127.0.0.1:8000/openapi.json" />
</rule>
<rule name="ProxyRemoveBG" stopProcessing="true">
<match url="^remove-bg(.*)" />
<action type="Rewrite" url="http://127.0.0.1:8000/remove-bg{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Now, I can call this api from any website, and if it’s in my CORS Whitelist, I will be allowed to use the api.
That’s it! I have attached the code. You can download the full code here.
How to use the code.
Download the Code.
Extract it to a folder.
Install Python if not installed on your computer or your online server.
Open Command Prompt, go to the directory where you have extracted the code and run the following code
python -m venv venv
Then run the following command
venv\Scripts\activate
Then run the following command
uvicorn main:app --host 127.0.0.1 --port 8000
At this point it should start working on http://127.0.0.1:8000/
Check by going to http://127.0.0.1:8000/docs