Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import uuid
|
3 |
+
import requests
|
4 |
+
from flask import Flask, request, send_file
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
app = Flask(__name__)
|
9 |
+
|
10 |
+
# Ensure temp directory exists
|
11 |
+
TEMP_DIR = "temp"
|
12 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
13 |
+
|
14 |
+
@app.route('/api')
|
15 |
+
def generate_profile():
|
16 |
+
# Get query parameters
|
17 |
+
name = request.args.get('name', 'Unknown')
|
18 |
+
rank = request.args.get('rank', 'Unranked')
|
19 |
+
balance = request.args.get('balance', '0')
|
20 |
+
next_rank = request.args.get('next_rank', 'Unknown')
|
21 |
+
pfp_url = request.args.get('pfp', None) # Profile picture URL
|
22 |
+
|
23 |
+
# Load background
|
24 |
+
bg = Image.open("bg.png").convert("RGBA")
|
25 |
+
draw = ImageDraw.Draw(bg)
|
26 |
+
|
27 |
+
# Load font
|
28 |
+
font_large = ImageFont.truetype("font.ttf", 50) # Larger font for name
|
29 |
+
font_small = ImageFont.truetype("font.ttf", 40) # Smaller font for details
|
30 |
+
|
31 |
+
# Get image size
|
32 |
+
W, H = bg.size
|
33 |
+
|
34 |
+
# Profile picture settings
|
35 |
+
pfp_size = 150 # Size of the profile picture
|
36 |
+
pfp_y = 50 # Position from top
|
37 |
+
pfp_x = (W - pfp_size) // 2 # Centering horizontally
|
38 |
+
|
39 |
+
# Load profile picture or fallback
|
40 |
+
try:
|
41 |
+
if pfp_url:
|
42 |
+
response = requests.get(pfp_url, timeout=5)
|
43 |
+
pfp = Image.open(BytesIO(response.content)).convert("RGBA")
|
44 |
+
else:
|
45 |
+
pfp = Image.open("fallback.png").convert("RGBA") # Fallback image
|
46 |
+
|
47 |
+
pfp = pfp.resize((pfp_size, pfp_size), Image.LANCZOS)
|
48 |
+
|
49 |
+
# Create circular mask for profile picture
|
50 |
+
mask = Image.new("L", (pfp_size, pfp_size), 0)
|
51 |
+
draw_mask = ImageDraw.Draw(mask)
|
52 |
+
draw_mask.ellipse((0, 0, pfp_size, pfp_size), fill=255)
|
53 |
+
|
54 |
+
# Apply mask and paste profile picture
|
55 |
+
pfp = Image.composite(pfp, Image.new("RGBA", pfp.size, (0, 0, 0, 0)), mask)
|
56 |
+
bg.paste(pfp, (pfp_x, pfp_y), pfp)
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Error loading profile picture: {e}")
|
59 |
+
|
60 |
+
# Define text positions
|
61 |
+
y_start = pfp_y + pfp_size + 30 # Start below the profile picture
|
62 |
+
spacing = 80 # Vertical gap between lines
|
63 |
+
text_color = (255, 255, 255) # White color
|
64 |
+
|
65 |
+
# Centered text positions
|
66 |
+
def draw_centered_text(text, y, font):
|
67 |
+
text_width, _ = draw.textsize(text, font=font)
|
68 |
+
x = (W - text_width) // 2 # Center the text
|
69 |
+
draw.text((x, y), text, font=font, fill=text_color)
|
70 |
+
|
71 |
+
# Draw text
|
72 |
+
draw_centered_text(f"Name: {name}", y_start, font_large)
|
73 |
+
draw_centered_text(f"Rank: {rank}", y_start + spacing, font_small)
|
74 |
+
draw_centered_text(f"Balance: {balance}", y_start + 2 * spacing, font_small)
|
75 |
+
draw_centered_text(f"Next Rank: {next_rank}", y_start + 3 * spacing, font_small)
|
76 |
+
|
77 |
+
# Generate a random filename
|
78 |
+
output_path = os.path.join(TEMP_DIR, f"profile_{uuid.uuid4().hex}.png")
|
79 |
+
bg.save(output_path)
|
80 |
+
|
81 |
+
# Send and delete image after sending
|
82 |
+
response = send_file(output_path, mimetype='image/png')
|
83 |
+
os.remove(output_path) # Clean up file after sending
|
84 |
+
return response
|
85 |
+
|
86 |
+
if __name__ == '__main__':
|
87 |
+
app.run(host='0.0.0.0', port=5000, debug=True)
|