feat: update remove-image-background route
Browse files
app.py
CHANGED
@@ -294,7 +294,7 @@ def split_image(image: Image.Image) -> List[Image.Image]:
|
|
294 |
return [preprocess_image(image) for image in images]
|
295 |
|
296 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
297 |
-
from fastapi.responses import JSONResponse, FileResponse
|
298 |
import tempfile
|
299 |
|
300 |
app = FastAPI()
|
@@ -338,7 +338,7 @@ async def generate_3d(image: UploadFile = File(...)):
|
|
338 |
raise HTTPException(status_code=500, detail=str(e))
|
339 |
|
340 |
@app.post("/remove-image-background")
|
341 |
-
async def
|
342 |
if not image:
|
343 |
raise HTTPException(status_code=400, detail="No image provided")
|
344 |
|
@@ -346,14 +346,18 @@ async def remove_background_image(image: UploadFile = File(...)):
|
|
346 |
image_data = Image.open(image.file)
|
347 |
image_data = image_data.convert("RGBA")
|
348 |
image_data = preprocess_image(image_data)
|
349 |
-
|
350 |
buffer = io.BytesIO()
|
351 |
-
|
352 |
buffer.seek(0)
|
353 |
|
354 |
-
return
|
|
|
|
|
|
|
|
|
355 |
|
356 |
except Exception as e:
|
|
|
357 |
raise HTTPException(status_code=500, detail=str(e))
|
358 |
|
359 |
|
|
|
294 |
return [preprocess_image(image) for image in images]
|
295 |
|
296 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
297 |
+
from fastapi.responses import JSONResponse, FileResponse, StreamingResponse
|
298 |
import tempfile
|
299 |
|
300 |
app = FastAPI()
|
|
|
338 |
raise HTTPException(status_code=500, detail=str(e))
|
339 |
|
340 |
@app.post("/remove-image-background")
|
341 |
+
async def remove_image_background(image: UploadFile = File(...)):
|
342 |
if not image:
|
343 |
raise HTTPException(status_code=400, detail="No image provided")
|
344 |
|
|
|
346 |
image_data = Image.open(image.file)
|
347 |
image_data = image_data.convert("RGBA")
|
348 |
image_data = preprocess_image(image_data)
|
|
|
349 |
buffer = io.BytesIO()
|
350 |
+
image_data.save(buffer, format="PNG")
|
351 |
buffer.seek(0)
|
352 |
|
353 |
+
return StreamingResponse(
|
354 |
+
content=buffer,
|
355 |
+
media_type='image/png',
|
356 |
+
headers={"Content-Disposition": "inline; filename=image.png"}
|
357 |
+
)
|
358 |
|
359 |
except Exception as e:
|
360 |
+
print(e)
|
361 |
raise HTTPException(status_code=500, detail=str(e))
|
362 |
|
363 |
|