-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageback.py
24 lines (22 loc) · 880 Bytes
/
imageback.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import io
from fastapi import FastAPI, File, UploadFile, APIRouter
from fastapi.middleware.cors import CORSMiddleware
import uuid
from PIL import Image
from rembg import remove
from fastapi.responses import FileResponse
import tempfile
async def removesbg(file: UploadFile = File(...)):
file.filename = f"{uuid.uuid4()}.jpg"
contents = await file.read()
image = Image.open(io.BytesIO(contents))
output_image = remove(image)
with tempfile.NamedTemporaryFile(delete=False) as tmp:
output_image.save(tmp, format = "png")
tmp.flush()
tmp.seek(0)
# background_color = (255,255,255)
# background_image = Image.new('RGB', output_image.size, background_color)
# background_image.paste(output_image, mask=output_image.convert('RGBA'))
# output_image.show()
return FileResponse(tmp.name, media_type="image/png")