Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,7 @@
|
|
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 |
|
@@ -11,6 +9,10 @@ app = Flask(__name__)
|
|
11 |
TEMP_DIR = "temp"
|
12 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
13 |
|
|
|
|
|
|
|
|
|
14 |
# Load fonts safely
|
15 |
def load_font(font_path, size):
|
16 |
try:
|
@@ -20,19 +22,20 @@ def load_font(font_path, size):
|
|
20 |
|
21 |
@app.route('/api')
|
22 |
def generate_profile():
|
23 |
-
""" Generate a profile card with user details and a profile picture. """
|
24 |
# Get query parameters
|
25 |
name = request.args.get('name', 'Unknown')
|
26 |
rank = request.args.get('rank', 'Unranked')
|
27 |
balance = request.args.get('balance', '0')
|
28 |
next_rank = request.args.get('next_rank', 'Unknown')
|
29 |
-
|
|
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
draw = ImageDraw.Draw(bg)
|
38 |
W, H = bg.size # Get background dimensions
|
@@ -47,44 +50,32 @@ def generate_profile():
|
|
47 |
pfp_x = 50
|
48 |
pfp_y = H // 2 - pfp_size // 2 # Center vertically
|
49 |
|
50 |
-
#
|
51 |
-
pfp_path =
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
border_size = 10
|
77 |
-
border = Image.new("RGBA", (pfp_size + border_size, pfp_size + border_size), (255, 255, 255, 255))
|
78 |
-
border_mask = Image.new("L", (pfp_size + border_size, pfp_size + border_size), 0)
|
79 |
-
border_draw = ImageDraw.Draw(border_mask)
|
80 |
-
border_draw.ellipse((0, 0, pfp_size + border_size, pfp_size + border_size), fill=255)
|
81 |
-
border.paste(circular_pfp, (border_size // 2, border_size // 2), mask)
|
82 |
-
|
83 |
-
# Paste profile picture onto background
|
84 |
-
bg.paste(border, (pfp_x - border_size // 2, pfp_y - border_size // 2), border_mask)
|
85 |
-
except Exception as e:
|
86 |
-
print(f"Error loading profile picture: {e}")
|
87 |
-
pfp_path = None # Ensure the file is not deleted if it wasn't created
|
88 |
|
89 |
# **TEXT POSITIONING**
|
90 |
text_x = pfp_x + pfp_size + 40 # Place text beside the profile picture
|
@@ -100,9 +91,10 @@ def generate_profile():
|
|
100 |
return font
|
101 |
|
102 |
# Adjust name font size dynamically
|
103 |
-
|
|
|
104 |
|
105 |
-
# **Draw text**
|
106 |
draw.text((text_x, text_y), name, font=name_font, fill=text_color)
|
107 |
draw.text((text_x, text_y + text_spacing), f"Rank: {rank}", font=font_medium, fill=text_color)
|
108 |
draw.text((text_x, text_y + 2 * text_spacing), f"Balance: {balance}", font=font_medium, fill=text_color)
|
@@ -118,8 +110,6 @@ def generate_profile():
|
|
118 |
# Cleanup saved files
|
119 |
if os.path.exists(output_path):
|
120 |
os.remove(output_path)
|
121 |
-
if pfp_path and os.path.exists(pfp_path): # Delete downloaded pfp if it was created
|
122 |
-
os.remove(pfp_path)
|
123 |
|
124 |
return response
|
125 |
|
|
|
1 |
import os
|
2 |
import uuid
|
|
|
3 |
from flask import Flask, request, send_file
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
|
|
9 |
TEMP_DIR = "temp"
|
10 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
11 |
|
12 |
+
# Available images
|
13 |
+
PFP_OPTIONS = {str(i): f"pfp{i}.png" for i in range(1, 6)}
|
14 |
+
BG_OPTIONS = {str(i): f"bg{i}.png" for i in range(1, 4)}
|
15 |
+
|
16 |
# Load fonts safely
|
17 |
def load_font(font_path, size):
|
18 |
try:
|
|
|
22 |
|
23 |
@app.route('/api')
|
24 |
def generate_profile():
|
25 |
+
""" Generate a profile card with user details and a chosen profile picture & background. """
|
26 |
# Get query parameters
|
27 |
name = request.args.get('name', 'Unknown')
|
28 |
rank = request.args.get('rank', 'Unranked')
|
29 |
balance = request.args.get('balance', '0')
|
30 |
next_rank = request.args.get('next_rank', 'Unknown')
|
31 |
+
pfp_choice = request.args.get('pfp', '1')
|
32 |
+
bg_choice = request.args.get('bg', '1')
|
33 |
|
34 |
+
# Validate and load background
|
35 |
+
bg_path = BG_OPTIONS.get(bg_choice, "bg1.png")
|
36 |
+
if not os.path.exists(bg_path):
|
37 |
+
return f"Error: Background '{bg_choice}' not found.", 400
|
38 |
+
bg = Image.open(bg_path).convert("RGBA")
|
39 |
|
40 |
draw = ImageDraw.Draw(bg)
|
41 |
W, H = bg.size # Get background dimensions
|
|
|
50 |
pfp_x = 50
|
51 |
pfp_y = H // 2 - pfp_size // 2 # Center vertically
|
52 |
|
53 |
+
# Validate and load profile picture
|
54 |
+
pfp_path = PFP_OPTIONS.get(pfp_choice, "pfp1.png")
|
55 |
+
if not os.path.exists(pfp_path):
|
56 |
+
return f"Error: Profile picture '{pfp_choice}' not found.", 400
|
57 |
+
pfp = Image.open(pfp_path).convert("RGBA")
|
58 |
+
|
59 |
+
# Resize and mask the profile picture (circular crop)
|
60 |
+
pfp = pfp.resize((pfp_size, pfp_size), Image.LANCZOS)
|
61 |
+
mask = Image.new("L", (pfp_size, pfp_size), 0)
|
62 |
+
mask_draw = ImageDraw.Draw(mask)
|
63 |
+
mask_draw.ellipse((0, 0, pfp_size, pfp_size), fill=255)
|
64 |
+
|
65 |
+
# Create a circular profile picture
|
66 |
+
circular_pfp = Image.new("RGBA", (pfp_size, pfp_size), (0, 0, 0, 0))
|
67 |
+
circular_pfp.paste(pfp, (0, 0), mask)
|
68 |
+
|
69 |
+
# Create a white border
|
70 |
+
border_size = 10
|
71 |
+
border = Image.new("RGBA", (pfp_size + border_size, pfp_size + border_size), (255, 255, 255, 255))
|
72 |
+
border_mask = Image.new("L", (pfp_size + border_size, pfp_size + border_size), 0)
|
73 |
+
border_draw = ImageDraw.Draw(border_mask)
|
74 |
+
border_draw.ellipse((0, 0, pfp_size + border_size, pfp_size + border_size), fill=255)
|
75 |
+
border.paste(circular_pfp, (border_size // 2, border_size // 2), mask)
|
76 |
+
|
77 |
+
# Paste profile picture onto background
|
78 |
+
bg.paste(border, (pfp_x - border_size // 2, pfp_y - border_size // 2), border_mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# **TEXT POSITIONING**
|
81 |
text_x = pfp_x + pfp_size + 40 # Place text beside the profile picture
|
|
|
91 |
return font
|
92 |
|
93 |
# Adjust name font size dynamically
|
94 |
+
max_text_width = W - text_x - 50
|
95 |
+
name_font = fit_text(name, max_text_width, font_large)
|
96 |
|
97 |
+
# **Draw text (Ensuring alignment)**
|
98 |
draw.text((text_x, text_y), name, font=name_font, fill=text_color)
|
99 |
draw.text((text_x, text_y + text_spacing), f"Rank: {rank}", font=font_medium, fill=text_color)
|
100 |
draw.text((text_x, text_y + 2 * text_spacing), f"Balance: {balance}", font=font_medium, fill=text_color)
|
|
|
110 |
# Cleanup saved files
|
111 |
if os.path.exists(output_path):
|
112 |
os.remove(output_path)
|
|
|
|
|
113 |
|
114 |
return response
|
115 |
|