Add Remove Background feature using AI (rembg)

Frontend:
- Added "Remove Background (AI)" to Image menu
- Created remove_background.js module with dialog options
- Added removeBackground method to API service

Backend:
- Added /tools/remove-background-base64 endpoint for miniPaint frontend
- Uses rembg library for AI-powered background removal

Features:
- Automatically detects main subject and removes background
- Option to create as new layer or replace current
- Enables transparency mode after removal
- Works with any image layer
This commit is contained in:
Claude
2026-01-27 00:47:22 +00:00
parent f650ec3019
commit 22d9f767a8
4 changed files with 221 additions and 0 deletions
+44
View File
@@ -33,6 +33,10 @@ class InpaintRequest(BaseModel):
guidance_scale: Optional[float] = 7.5
class RemoveBackgroundRequest(BaseModel):
image: str # Base64 encoded image
@router.post("/smart-select-base64")
async def smart_select_base64(request: SmartSelectRequest):
"""
@@ -124,6 +128,46 @@ async def inpaint_base64(request: InpaintRequest):
raise HTTPException(status_code=500, detail=str(e))
@router.post("/remove-background-base64")
async def remove_background_base64(request: RemoveBackgroundRequest):
"""
Remove background from a base64 encoded image using rembg.
Returns base64 encoded PNG with transparent background.
Used by miniPaint frontend.
"""
try:
from rembg import remove
except ImportError:
raise HTTPException(
status_code=500,
detail="rembg not installed. Run: pip install rembg"
)
try:
# Decode base64 image
image_bytes = base64.b64decode(request.image)
# Remove background
result_bytes = remove(image_bytes)
# Convert result to base64
result_b64 = base64.b64encode(result_bytes).decode('utf-8')
# Get dimensions
img = Image.open(BytesIO(result_bytes))
return {
"result": result_b64,
"width": img.width,
"height": img.height
}
except Exception as e:
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
@router.post("/remove-background")
async def remove_background(
project_id: Optional[int] = Form(None),