Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, send_file
|
| 2 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
+
import urllib.parse
|
| 4 |
+
import uuid
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
@app.route('/leaderboard/<path:names>')
|
| 10 |
+
def leaderboard(names):
|
| 11 |
+
# Decode URL-encoded names (handling spaces)
|
| 12 |
+
decoded_names = urllib.parse.unquote(names)
|
| 13 |
+
name_list = decoded_names.split('-')
|
| 14 |
+
|
| 15 |
+
if len(name_list) != 10:
|
| 16 |
+
return "Error: Exactly 10 names required", 400
|
| 17 |
+
|
| 18 |
+
# Load background image
|
| 19 |
+
bg = Image.open("image.png").convert("RGBA")
|
| 20 |
+
draw = ImageDraw.Draw(bg)
|
| 21 |
+
|
| 22 |
+
# Load font (adjust path and size)
|
| 23 |
+
font = ImageFont.truetype("arial.ttf", 40)
|
| 24 |
+
|
| 25 |
+
# Positioning variables
|
| 26 |
+
start_x, start_y = 50, 100
|
| 27 |
+
line_spacing = 50
|
| 28 |
+
|
| 29 |
+
# Draw names on image
|
| 30 |
+
for i, name in enumerate(name_list):
|
| 31 |
+
draw.text((start_x, start_y + i * line_spacing), f"{i+1}. {name}", font=font, fill="white")
|
| 32 |
+
|
| 33 |
+
# Generate unique filename
|
| 34 |
+
output_filename = f"output_{uuid.uuid4().hex}.png"
|
| 35 |
+
output_path = os.path.join("output", output_filename)
|
| 36 |
+
|
| 37 |
+
# Ensure output directory exists
|
| 38 |
+
os.makedirs("output", exist_ok=True)
|
| 39 |
+
|
| 40 |
+
# Save the generated image
|
| 41 |
+
bg.save(output_path)
|
| 42 |
+
|
| 43 |
+
return send_file(output_path, mimetype='image/png')
|
| 44 |
+
|
| 45 |
+
if __name__ == '__main__':
|
| 46 |
+
app.run(debug=True)
|