File size: 695 Bytes
569d4a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from fastapi import FastAPI, UploadFile, File
from rembg import remove
from io import BytesIO
from starlette.responses import Response

# Membuat aplikasi
app = FastAPI()

@app.get("/")
def home():
    return {"message": "Halo! API Penghapus Background siap digunakan."}

# Ini fitur utamanya
@app.post("/hapus-background")
async def hapus_background(file: UploadFile = File(...)):
    # 1. Baca gambar yang diupload user
    gambar_asli = await file.read()
    
    # 2. Proses hapus background pakai rembg
    gambar_bersih = remove(gambar_asli)
    
    # 3. Kembalikan hasilnya sebagai gambar PNG
    return Response(content=gambar_bersih, media_type="image/png")