Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,38 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import StreamingResponse
|
| 3 |
+
from PIL import Image, ImageOps, ImageFilter
|
| 4 |
+
import io
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
@app.post("/")
|
| 10 |
+
async def convert_to_sketch(file: UploadFile = File(...)):
|
| 11 |
+
# Load the image
|
| 12 |
+
image = Image.open(io.BytesIO(await file.read()))
|
| 13 |
|
| 14 |
+
# Convert to grayscale
|
| 15 |
+
grayscale = ImageOps.grayscale(image)
|
| 16 |
|
| 17 |
+
# Invert the grayscale image
|
| 18 |
+
inverted = ImageOps.invert(grayscale)
|
| 19 |
|
| 20 |
+
# Apply Gaussian blur to the inverted image
|
| 21 |
+
blurred = inverted.filter(ImageFilter.GaussianBlur(15))
|
| 22 |
+
|
| 23 |
+
# Blend the grayscale image with the blurred inverted image using color dodge
|
| 24 |
+
blended = Image.blend(grayscale, blurred, 0.5)
|
| 25 |
+
|
| 26 |
+
# Enhance the contrast (optional)
|
| 27 |
+
enhanced = ImageOps.autocontrast(blended, cutoff=2)
|
| 28 |
+
|
| 29 |
+
# Save the resulting image to a bytes buffer
|
| 30 |
+
buf = io.BytesIO()
|
| 31 |
+
enhanced.save(buf, format='PNG')
|
| 32 |
+
buf.seek(0)
|
| 33 |
+
|
| 34 |
+
return StreamingResponse(buf, media_type="image/png")
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
import uvicorn
|
| 38 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|