Spaces:
Sleeping
Sleeping
Download app.py from raddiv/image-upscaler-api: direct link, hf CLI and curl.
- Browser
- Download file 743 Bytes
-
https://huggingface.co/spaces/raddiv/image-upscaler-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/raddiv/image-upscaler-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/raddiv/image-upscaler-api/resolve/main/app.py
743 Bytes
| from fastapi import FastAPI, UploadFile, File | |
| from PIL import Image | |
| import io, base64 | |
| app = FastAPI() | |
| async def upscale(file: UploadFile = File(...), scale: int = 2): | |
| input_bytes = await file.read() | |
| image = Image.open(io.BytesIO(input_bytes)).convert("RGB") | |
| w, h = image.size | |
| new_size = (w * scale, h * scale) | |
| upscaled = image.resize(new_size, Image.LANCZOS) | |
| buf = io.BytesIO() | |
| upscaled.save(buf, format="PNG") | |
| img_b64 = base64.b64encode(buf.getvalue()).decode() | |
| return {"image_base64": img_b64, "format": "png", "scale": scale, | |
| "original_size": f"{w}x{h}", "new_size": f"{new_size[0]}x{new_size[1]}"} | |
| async def health(): | |
| return {"status": "ok"} |