Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,13 +11,12 @@ 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:
|
17 |
return ImageFont.truetype(font_path, size)
|
18 |
except IOError:
|
19 |
print(f"Warning: Font {font_path} not found. Using default Arial.")
|
20 |
-
return ImageFont.truetype("arial.ttf", size)
|
21 |
|
22 |
@app.route('/api')
|
23 |
def generate_profile():
|
@@ -26,39 +25,43 @@ def generate_profile():
|
|
26 |
rank = request.args.get('rank', 'Unranked')
|
27 |
balance = request.args.get('balance', '0')
|
28 |
next_rank = request.args.get('next_rank', 'Unknown')
|
29 |
-
pfp_url = request.args.get('pfp', None)
|
30 |
|
31 |
-
# Load background
|
32 |
try:
|
33 |
-
bg = Image.open("bg.png").convert("RGB") # Convert to RGB
|
34 |
except IOError:
|
35 |
return "Error: Background image 'bg.png' not found.", 500
|
36 |
-
|
37 |
draw = ImageDraw.Draw(bg)
|
|
|
38 |
|
39 |
-
# Load fonts
|
40 |
font_large = load_font("font.ttf", 50)
|
41 |
font_small = load_font("font.ttf", 40)
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
|
46 |
# Profile picture settings
|
47 |
-
pfp_size = 150
|
48 |
-
pfp_y = 50
|
49 |
-
pfp_x = (W - pfp_size) // 2
|
50 |
|
51 |
-
# Load profile picture
|
52 |
try:
|
53 |
if pfp_url:
|
54 |
response = requests.get(pfp_url, timeout=5)
|
55 |
-
|
|
|
|
|
|
|
56 |
else:
|
57 |
-
pfp = Image.open("fallback.png").convert("RGBA")
|
58 |
|
59 |
pfp = pfp.resize((pfp_size, pfp_size), Image.LANCZOS)
|
60 |
|
61 |
-
# Create circular mask
|
62 |
mask = Image.new("L", (pfp_size, pfp_size), 0)
|
63 |
draw_mask = ImageDraw.Draw(mask)
|
64 |
draw_mask.ellipse((0, 0, pfp_size, pfp_size), fill=255)
|
@@ -66,21 +69,22 @@ def generate_profile():
|
|
66 |
# Apply mask and paste profile picture
|
67 |
pfp = Image.composite(pfp, Image.new("RGBA", pfp.size, (0, 0, 0, 0)), mask)
|
68 |
bg.paste(pfp, (pfp_x, pfp_y), pfp)
|
|
|
69 |
except Exception as e:
|
70 |
print(f"Error loading profile picture: {e}")
|
71 |
|
72 |
# Define text positions
|
73 |
-
y_start = pfp_y + pfp_size + 30
|
74 |
-
spacing = 80
|
75 |
-
text_color = (173, 216, 230)
|
76 |
|
77 |
-
#
|
78 |
def draw_centered_text(text, y, font):
|
79 |
try:
|
80 |
-
bbox = draw.textbbox((0, 0), text, font=font)
|
81 |
text_width = bbox[2] - bbox[0]
|
82 |
except AttributeError:
|
83 |
-
text_width, _ = draw.textsize(text, font=font)
|
84 |
|
85 |
x = (W - text_width) // 2
|
86 |
draw.text((x, y), text, font=font, fill=text_color)
|
@@ -91,13 +95,15 @@ def generate_profile():
|
|
91 |
draw_centered_text(f"Balance: {balance}", y_start + 2 * spacing, font_small)
|
92 |
draw_centered_text(f"Next Rank: {next_rank}", y_start + 3 * spacing, font_small)
|
93 |
|
94 |
-
|
|
|
|
|
95 |
output_path = os.path.join(TEMP_DIR, f"profile_{uuid.uuid4().hex}.png")
|
96 |
bg.save(output_path)
|
97 |
|
98 |
-
# Send and delete image
|
99 |
response = send_file(output_path, mimetype='image/png')
|
100 |
-
os.remove(output_path)
|
101 |
return response
|
102 |
|
103 |
if __name__ == '__main__':
|
|
|
11 |
TEMP_DIR = "temp"
|
12 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
13 |
|
|
|
14 |
def load_font(font_path, size):
|
15 |
try:
|
16 |
return ImageFont.truetype(font_path, size)
|
17 |
except IOError:
|
18 |
print(f"Warning: Font {font_path} not found. Using default Arial.")
|
19 |
+
return ImageFont.truetype("arial.ttf", size)
|
20 |
|
21 |
@app.route('/api')
|
22 |
def generate_profile():
|
|
|
25 |
rank = request.args.get('rank', 'Unranked')
|
26 |
balance = request.args.get('balance', '0')
|
27 |
next_rank = request.args.get('next_rank', 'Unknown')
|
28 |
+
pfp_url = request.args.get('pfp', None)
|
29 |
|
30 |
+
# Load background image
|
31 |
try:
|
32 |
+
bg = Image.open("bg.png").convert("RGB") # Convert to RGB
|
33 |
except IOError:
|
34 |
return "Error: Background image 'bg.png' not found.", 500
|
35 |
+
|
36 |
draw = ImageDraw.Draw(bg)
|
37 |
+
W, H = bg.size # Get background dimensions
|
38 |
|
39 |
+
# Load fonts
|
40 |
font_large = load_font("font.ttf", 50)
|
41 |
font_small = load_font("font.ttf", 40)
|
42 |
|
43 |
+
# Debugging: Check if text will be drawn
|
44 |
+
print(f"Drawing text on bg.png with dimensions: {W}x{H}")
|
45 |
|
46 |
# Profile picture settings
|
47 |
+
pfp_size = 150
|
48 |
+
pfp_y = 50
|
49 |
+
pfp_x = (W - pfp_size) // 2
|
50 |
|
51 |
+
# Load profile picture
|
52 |
try:
|
53 |
if pfp_url:
|
54 |
response = requests.get(pfp_url, timeout=5)
|
55 |
+
if response.status_code == 200:
|
56 |
+
pfp = Image.open(BytesIO(response.content)).convert("RGBA")
|
57 |
+
else:
|
58 |
+
raise ValueError("Invalid profile picture URL")
|
59 |
else:
|
60 |
+
pfp = Image.open("fallback.png").convert("RGBA")
|
61 |
|
62 |
pfp = pfp.resize((pfp_size, pfp_size), Image.LANCZOS)
|
63 |
|
64 |
+
# Create circular mask
|
65 |
mask = Image.new("L", (pfp_size, pfp_size), 0)
|
66 |
draw_mask = ImageDraw.Draw(mask)
|
67 |
draw_mask.ellipse((0, 0, pfp_size, pfp_size), fill=255)
|
|
|
69 |
# Apply mask and paste profile picture
|
70 |
pfp = Image.composite(pfp, Image.new("RGBA", pfp.size, (0, 0, 0, 0)), mask)
|
71 |
bg.paste(pfp, (pfp_x, pfp_y), pfp)
|
72 |
+
print("Profile picture successfully added.")
|
73 |
except Exception as e:
|
74 |
print(f"Error loading profile picture: {e}")
|
75 |
|
76 |
# Define text positions
|
77 |
+
y_start = pfp_y + pfp_size + 30
|
78 |
+
spacing = 80
|
79 |
+
text_color = (173, 216, 230)
|
80 |
|
81 |
+
# Function to center text
|
82 |
def draw_centered_text(text, y, font):
|
83 |
try:
|
84 |
+
bbox = draw.textbbox((0, 0), text, font=font)
|
85 |
text_width = bbox[2] - bbox[0]
|
86 |
except AttributeError:
|
87 |
+
text_width, _ = draw.textsize(text, font=font)
|
88 |
|
89 |
x = (W - text_width) // 2
|
90 |
draw.text((x, y), text, font=font, fill=text_color)
|
|
|
95 |
draw_centered_text(f"Balance: {balance}", y_start + 2 * spacing, font_small)
|
96 |
draw_centered_text(f"Next Rank: {next_rank}", y_start + 3 * spacing, font_small)
|
97 |
|
98 |
+
print("Text successfully drawn on image.")
|
99 |
+
|
100 |
+
# Save image
|
101 |
output_path = os.path.join(TEMP_DIR, f"profile_{uuid.uuid4().hex}.png")
|
102 |
bg.save(output_path)
|
103 |
|
104 |
+
# Send and delete image
|
105 |
response = send_file(output_path, mimetype='image/png')
|
106 |
+
os.remove(output_path)
|
107 |
return response
|
108 |
|
109 |
if __name__ == '__main__':
|