3pixel/main.py

45 lines
1.5 KiB
Python
Raw Permalink Normal View History

from fastapi import FastAPI, File, UploadFile, Response, HTTPException
from fastapi.responses import HTMLResponse
from img_proc import pixelate, img2byte
import fileutils as fu
app = FastAPI()
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def root_html():
return """
<html>
<head>
<title>pixelate via API</title>
</head>
<body>
<h2>pixelate</h2>
<strong> go to <a href="/docs"> /docs</a> for OpenAPI docs </strong>
<p>based on: <a href="https://github.com/ferretj/pixelate">https://github.com/ferretj/pixelate</a> </p>
</body>
</html>
"""
@app.post("/")
async def root(saturation: float = 1.5,
contrast: float = 1.2,
n_colors: int = 10,
superpixel_size: int = 15,
file: UploadFile = File(...)):
"""
saturation: float = 1.25,
contrast: float = 1.2,
n_colors: int = 10,
superpixel_size: int = 10,
"""
if not fu.has_valid_suffix(file.filename):
raise HTTPException(status_code=400,
detail="Invalid file ending! Only {} accepted".fomrat(fs.VALID_FILE_FORMATS))
out_img = pixelate(file.file,
saturation = saturation,
contrast = contrast,
n_colors = n_colors,
superpixel_size = superpixel_size)
return Response(content=img2byte(out_img), media_type="image/png")