-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
59 lines (46 loc) · 1.43 KB
/
server.py
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
# Copyright 2022 iiPython
# ROBLOX Image Loader v2
# Modules
import os
import zlib
import requests
from PIL import Image
from io import BytesIO
from flask import Flask, abort, request, render_template
# Initialization
app = Flask("Image Loader v2")
# Image loader
def load_image(url: str) -> bytes | None:
try:
req = requests.get(url, timeout = 2)
if req.status_code != 200:
return None
except requests.Timeout:
return None
# Handle image processing
image, image_data = Image.open(BytesIO(req.content)).convert("RGBA"), ""
image.thumbnail((500, 500), Image.ANTIALIAS)
for x in range(image.size[0]):
for y in range(image.size[1]):
c = image.getpixel((x, y))
image_data += f"{x};{y};{c[0]};{c[1]};{c[2]};{c[3]}:"
return zlib.compress(image_data[:-1].encode("utf-8"), 5)
# Routes
@app.route("/")
def index() -> None:
return render_template("index.html"), 200
@app.route("/status")
def status() -> None:
return "200 OK", 200
@app.route("/load", methods = ["POST"])
def process_image() -> None:
try:
url = request.data.decode("utf-8")
except Exception:
return abort(400)
image_data = load_image(url)
if image_data is None:
return abort(400)
return image_data
# Launch server
app.run(host = os.getenv("HOST", "0.0.0.0"), port = os.getenv("PORT", 8080))