API to pixelate an image
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

44 lines
1.5 KiB

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")