Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -47,12 +47,16 @@ def generate_profile():
|
|
47 |
pfp_x = 50
|
48 |
pfp_y = H // 2 - pfp_size // 2 # Center vertically
|
49 |
|
50 |
-
#
|
|
|
|
|
51 |
try:
|
52 |
if pfp_url:
|
53 |
response = requests.get(pfp_url, timeout=5)
|
54 |
if response.status_code == 200:
|
55 |
-
|
|
|
|
|
56 |
else:
|
57 |
raise ValueError("Invalid profile picture URL")
|
58 |
else:
|
@@ -76,12 +80,13 @@ def generate_profile():
|
|
76 |
border_draw.ellipse((0, 0, pfp_size + border_size, pfp_size + border_size), fill=255)
|
77 |
border.paste(circular_pfp, (border_size // 2, border_size // 2), mask)
|
78 |
|
79 |
-
# Paste profile picture onto background
|
80 |
bg.paste(border, (pfp_x - border_size // 2, pfp_y - border_size // 2), border_mask)
|
81 |
except Exception as e:
|
82 |
print(f"Error loading profile picture: {e}")
|
|
|
83 |
|
84 |
-
# **TEXT POSITIONING
|
85 |
text_x = pfp_x + pfp_size + 40 # Place text beside the profile picture
|
86 |
text_y = pfp_y # Align with profile picture
|
87 |
text_spacing = 55 # Space between text lines
|
@@ -97,7 +102,7 @@ def generate_profile():
|
|
97 |
# Adjust name font size dynamically
|
98 |
name_font = fit_text(name, W - text_x - 50, font_large)
|
99 |
|
100 |
-
# **Draw text
|
101 |
draw.text((text_x, text_y), name, font=name_font, fill=text_color)
|
102 |
draw.text((text_x, text_y + text_spacing), f"Rank: {rank}", font=font_medium, fill=text_color)
|
103 |
draw.text((text_x, text_y + 2 * text_spacing), f"Balance: {balance}", font=font_medium, fill=text_color)
|
@@ -107,12 +112,14 @@ def generate_profile():
|
|
107 |
output_path = os.path.join(TEMP_DIR, f"profile_{uuid.uuid4().hex}.png")
|
108 |
bg.save(output_path)
|
109 |
|
110 |
-
# Send
|
111 |
response = send_file(output_path, mimetype='image/png')
|
112 |
|
113 |
# Cleanup saved files
|
114 |
if os.path.exists(output_path):
|
115 |
os.remove(output_path)
|
|
|
|
|
116 |
|
117 |
return response
|
118 |
|
|
|
47 |
pfp_x = 50
|
48 |
pfp_y = H // 2 - pfp_size // 2 # Center vertically
|
49 |
|
50 |
+
# Download and save profile picture if provided
|
51 |
+
pfp_path = os.path.join(TEMP_DIR, f"pfp_{uuid.uuid4().hex}.png")
|
52 |
+
|
53 |
try:
|
54 |
if pfp_url:
|
55 |
response = requests.get(pfp_url, timeout=5)
|
56 |
if response.status_code == 200:
|
57 |
+
with open(pfp_path, "wb") as f:
|
58 |
+
f.write(response.content)
|
59 |
+
pfp = Image.open(pfp_path).convert("RGBA")
|
60 |
else:
|
61 |
raise ValueError("Invalid profile picture URL")
|
62 |
else:
|
|
|
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
|
91 |
text_y = pfp_y # Align with profile picture
|
92 |
text_spacing = 55 # Space between text lines
|
|
|
102 |
# Adjust name font size dynamically
|
103 |
name_font = fit_text(name, W - text_x - 50, font_large)
|
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)
|
|
|
112 |
output_path = os.path.join(TEMP_DIR, f"profile_{uuid.uuid4().hex}.png")
|
113 |
bg.save(output_path)
|
114 |
|
115 |
+
# Send image
|
116 |
response = send_file(output_path, mimetype='image/png')
|
117 |
|
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 |
|