Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -30,7 +30,7 @@ def generate_profile():
|
|
30 |
|
31 |
# Load background image
|
32 |
try:
|
33 |
-
bg = Image.open("bg.png").convert("RGBA") # Ensure
|
34 |
print("Background image loaded successfully.")
|
35 |
except IOError:
|
36 |
return "Error: Background image 'bg.png' not found.", 500
|
@@ -40,11 +40,12 @@ def generate_profile():
|
|
40 |
print(f"Canvas Size: {W}x{H}")
|
41 |
|
42 |
# Load fonts
|
43 |
-
font_large = load_font("font.ttf",
|
44 |
-
|
|
|
45 |
|
46 |
# Profile picture settings
|
47 |
-
pfp_size =
|
48 |
pfp_y = 50
|
49 |
pfp_x = (W - pfp_size) // 2
|
50 |
pfp_path = os.path.join(TEMP_DIR, f"pfp_{uuid.uuid4().hex}.png")
|
@@ -62,28 +63,32 @@ def generate_profile():
|
|
62 |
pfp = Image.open("fallback.png").convert("RGBA")
|
63 |
print("Using fallback profile picture.")
|
64 |
|
65 |
-
# Resize
|
66 |
pfp = pfp.resize((pfp_size, pfp_size), Image.LANCZOS)
|
67 |
-
pfp.save(pfp_path) # Save before using
|
68 |
-
print(f"Profile picture saved at {pfp_path}")
|
69 |
|
70 |
# Create circular mask
|
71 |
mask = Image.new("L", (pfp_size, pfp_size), 0)
|
72 |
draw_mask = ImageDraw.Draw(mask)
|
73 |
draw_mask.ellipse((0, 0, pfp_size, pfp_size), fill=255)
|
74 |
|
75 |
-
#
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
print("Profile picture successfully added.")
|
80 |
except Exception as e:
|
81 |
print(f"Error loading profile picture: {e}")
|
82 |
|
83 |
# Define text positions
|
84 |
-
|
85 |
-
|
86 |
-
text_color = (255, 255, 255) # White for
|
87 |
|
88 |
# Function to center text
|
89 |
def draw_centered_text(text, y, font):
|
@@ -96,12 +101,12 @@ def generate_profile():
|
|
96 |
x = (W - text_width) // 2
|
97 |
draw.text((x, y), text, font=font, fill=text_color)
|
98 |
|
99 |
-
# Draw text
|
100 |
print("Drawing text...")
|
101 |
-
draw_centered_text(f"
|
102 |
-
draw_centered_text(f"Rank: {rank}",
|
103 |
-
draw_centered_text(f"Balance: {balance}",
|
104 |
-
draw_centered_text(f"Next Rank: {next_rank}",
|
105 |
print("Text successfully drawn on image.")
|
106 |
|
107 |
# Save final image
|
|
|
30 |
|
31 |
# Load background image
|
32 |
try:
|
33 |
+
bg = Image.open("bg.png").convert("RGBA") # Ensure transparency support
|
34 |
print("Background image loaded successfully.")
|
35 |
except IOError:
|
36 |
return "Error: Background image 'bg.png' not found.", 500
|
|
|
40 |
print(f"Canvas Size: {W}x{H}")
|
41 |
|
42 |
# Load fonts
|
43 |
+
font_large = load_font("font.ttf", 45) # Large title font
|
44 |
+
font_medium = load_font("font.ttf", 35) # Medium-sized font
|
45 |
+
font_small = load_font("font.ttf", 30) # Small text font
|
46 |
|
47 |
# Profile picture settings
|
48 |
+
pfp_size = 170
|
49 |
pfp_y = 50
|
50 |
pfp_x = (W - pfp_size) // 2
|
51 |
pfp_path = os.path.join(TEMP_DIR, f"pfp_{uuid.uuid4().hex}.png")
|
|
|
63 |
pfp = Image.open("fallback.png").convert("RGBA")
|
64 |
print("Using fallback profile picture.")
|
65 |
|
66 |
+
# Resize profile picture
|
67 |
pfp = pfp.resize((pfp_size, pfp_size), Image.LANCZOS)
|
|
|
|
|
68 |
|
69 |
# Create circular mask
|
70 |
mask = Image.new("L", (pfp_size, pfp_size), 0)
|
71 |
draw_mask = ImageDraw.Draw(mask)
|
72 |
draw_mask.ellipse((0, 0, pfp_size, pfp_size), fill=255)
|
73 |
|
74 |
+
# Create a white border
|
75 |
+
border_size = 8
|
76 |
+
border = Image.new("RGBA", (pfp_size + border_size, pfp_size + border_size), (255, 255, 255, 255))
|
77 |
+
border_mask = Image.new("L", (pfp_size + border_size, pfp_size + border_size), 0)
|
78 |
+
border_draw = ImageDraw.Draw(border_mask)
|
79 |
+
border_draw.ellipse((0, 0, pfp_size + border_size, pfp_size + border_size), fill=255)
|
80 |
+
border.paste(pfp, (border_size // 2, border_size // 2), mask)
|
81 |
+
|
82 |
+
# Paste profile picture with border
|
83 |
+
bg.paste(border, (pfp_x - border_size // 2, pfp_y - border_size // 2), border_mask)
|
84 |
print("Profile picture successfully added.")
|
85 |
except Exception as e:
|
86 |
print(f"Error loading profile picture: {e}")
|
87 |
|
88 |
# Define text positions
|
89 |
+
text_y_start = pfp_y + pfp_size + 40 # Start below the profile picture
|
90 |
+
text_spacing = 60
|
91 |
+
text_color = (255, 255, 255) # White text for better contrast
|
92 |
|
93 |
# Function to center text
|
94 |
def draw_centered_text(text, y, font):
|
|
|
101 |
x = (W - text_width) // 2
|
102 |
draw.text((x, y), text, font=font, fill=text_color)
|
103 |
|
104 |
+
# Draw text neatly aligned
|
105 |
print("Drawing text...")
|
106 |
+
draw_centered_text(f"{name}", text_y_start, font_large)
|
107 |
+
draw_centered_text(f"Rank: {rank}", text_y_start + text_spacing, font_medium)
|
108 |
+
draw_centered_text(f"Balance: {balance}", text_y_start + 2 * text_spacing, font_medium)
|
109 |
+
draw_centered_text(f"Next Rank: {next_rank}", text_y_start + 3 * text_spacing, font_small)
|
110 |
print("Text successfully drawn on image.")
|
111 |
|
112 |
# Save final image
|