-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (67 loc) · 2.77 KB
/
app.py
File metadata and controls
82 lines (67 loc) · 2.77 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from fastapi import FastAPI, File, UploadFile, HTTPException, Header, Query
from fastapi.responses import StreamingResponse, PlainTextResponse
from PIL import Image, ImageOps
from io import BytesIO
import os
import imghdr
API_KEY = os.getenv("API_KEY", "")
app = FastAPI(title="Image Normalizer", version="1.0.0")
def check_api_key(x_api_key: str | None):
if not API_KEY:
return
if x_api_key is None or x_api_key != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
@app.get("/health", response_class=PlainTextResponse)
def health():
return "OK"
@app.post("/normalize")
async def normalize_image(
image: UploadFile = File(...),
x_api_key: str | None = Header(default=None, convert_underscores=False),
fmt: str = Query(default="keep"), # keep|jpeg|png|webp
quality: int = Query(default=90, ge=1, le=100),
strip: bool = Query(default=True),
optimize: bool = Query(default=True),
):
check_api_key(x_api_key)
data = await image.read()
if not data:
raise HTTPException(400, "Empty file")
kind = imghdr.what(None, h=data)
if kind not in ("jpeg", "png", "webp", "tiff", "bmp", "gif", None):
raise HTTPException(415, f"Unsupported image type: {kind}")
try:
im = Image.open(BytesIO(data))
except Exception as e:
raise HTTPException(415, f"Cannot open image: {e}")
# применяем EXIF-поворот и тем самым нормализуем пиксели
im = ImageOps.exif_transpose(im)
in_format = (im.format or "").lower()
if fmt == "keep":
out_format = in_format if in_format in ("jpeg", "png", "webp") else "jpeg"
else:
out_format = fmt.lower()
if out_format not in ("jpeg", "png", "webp"):
raise HTTPException(400, "fmt must be keep|jpeg|png|webp")
save_params = {}
if out_format in ("jpeg", "webp"):
save_params["quality"] = quality
if strip:
im.info.pop("exif", None)
if optimize:
save_params["optimize"] = True
if out_format == "jpeg":
save_params.setdefault("progressive", True)
save_params.setdefault("subsampling", "4:2:0")
if out_format == "jpeg" and im.mode in ("RGBA", "LA", "P"):
bg = Image.new("RGB", im.size, (255, 255, 255))
bg.paste(im, mask=im.split()[-1] if im.mode in ("RGBA", "LA") else None)
im = bg
elif out_format in ("png", "webp") and im.mode == "P":
im = im.convert("RGBA")
buf = BytesIO()
im.save(buf, format=out_format.upper(), **save_params)
buf.seek(0)
mime = {"jpeg":"image/jpeg","png":"image/png","webp":"image/webp"}[out_format]
headers = {"Cache-Control":"no-store","X-Processed":"auto-orient,strip"}
return StreamingResponse(buf, media_type=mime, headers=headers)