Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
import urllib.parse
|
| 4 |
import uuid
|
|
@@ -10,25 +10,42 @@ app = Flask(__name__)
|
|
| 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)
|
| 16 |
-
return "Error:
|
| 17 |
|
| 18 |
# Load background image
|
| 19 |
bg = Image.open("image.png").convert("RGBA")
|
| 20 |
draw = ImageDraw.Draw(bg)
|
| 21 |
|
| 22 |
-
# Load font (
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Draw names on image
|
| 30 |
-
for i, name in enumerate(name_list):
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
# Generate unique filename
|
| 34 |
output_filename = f"output_{uuid.uuid4().hex}.png"
|
|
|
|
| 1 |
+
from flask import Flask, send_file
|
| 2 |
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
import urllib.parse
|
| 4 |
import uuid
|
|
|
|
| 10 |
def leaderboard(names):
|
| 11 |
# Decode URL-encoded names (handling spaces)
|
| 12 |
decoded_names = urllib.parse.unquote(names)
|
| 13 |
+
name_list = [name.strip() for name in decoded_names.split('-') if name.strip()]
|
| 14 |
|
| 15 |
+
if len(name_list) == 0:
|
| 16 |
+
return "Error: At least 1 name required", 400
|
| 17 |
|
| 18 |
# Load background image
|
| 19 |
bg = Image.open("image.png").convert("RGBA")
|
| 20 |
draw = ImageDraw.Draw(bg)
|
| 21 |
|
| 22 |
+
# Load Gagalin font (Ensure the font file exists in the project directory)
|
| 23 |
+
font_path = "gagalin.otf"
|
| 24 |
+
if not os.path.exists(font_path):
|
| 25 |
+
return "Error: Font file 'Gagalin.otf' not found", 500
|
| 26 |
|
| 27 |
+
font = ImageFont.truetype(font_path, 50) # Adjust font size as needed
|
| 28 |
+
|
| 29 |
+
# Image dimensions
|
| 30 |
+
img_width, img_height = bg.size
|
| 31 |
+
|
| 32 |
+
# Determine spacing based on the number of names
|
| 33 |
+
total_names = len(name_list)
|
| 34 |
+
max_names = 10
|
| 35 |
+
|
| 36 |
+
if total_names == 3: # Special case: Center top 3
|
| 37 |
+
start_y = 100
|
| 38 |
+
line_spacing = 80
|
| 39 |
+
else:
|
| 40 |
+
start_y = 150
|
| 41 |
+
line_spacing = (img_height - 300) // max_names
|
| 42 |
+
|
| 43 |
+
start_x = img_width // 5 # Adjust horizontal alignment
|
| 44 |
|
| 45 |
# Draw names on image
|
| 46 |
+
for i, name in enumerate(name_list[:max_names]):
|
| 47 |
+
position = (start_x, start_y + i * line_spacing)
|
| 48 |
+
draw.text(position, f"{i+1}. {name}", font=font, fill="white")
|
| 49 |
|
| 50 |
# Generate unique filename
|
| 51 |
output_filename = f"output_{uuid.uuid4().hex}.png"
|