testing1 / app.py
mrbeliever's picture
Update app.py
d0c68f0 verified
Raw
History Blame Contribute Delete
1.08 kB
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)