File size: 1,080 Bytes
09f16a9
 
 
 
1dfbac3
 
 
09f16a9
 
 
 
1dfbac3
09f16a9
 
1dfbac3
09f16a9
 
1dfbac3
09f16a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import StreamingResponse
from PIL import Image, ImageOps, ImageFilter
import io

app = FastAPI()

@app.post("/")
async def convert_to_sketch(file: UploadFile = File(...)):
    # Load the image
    image = Image.open(io.BytesIO(await file.read()))

    # Convert to grayscale
    grayscale = ImageOps.grayscale(image)

    # Invert the grayscale image
    inverted = ImageOps.invert(grayscale)

    # Apply Gaussian blur to the inverted image
    blurred = inverted.filter(ImageFilter.GaussianBlur(15))

    # Blend the grayscale image with the blurred inverted image using color dodge
    blended = Image.blend(grayscale, blurred, 0.5)

    # Enhance the contrast (optional)
    enhanced = ImageOps.autocontrast(blended, cutoff=2)

    # Save the resulting image to a bytes buffer
    buf = io.BytesIO()
    enhanced.save(buf, format='PNG')
    buf.seek(0)

    return StreamingResponse(buf, media_type="image/png")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)