Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,7 @@ 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:
|
@@ -29,37 +30,41 @@ def generate_profile():
|
|
29 |
|
30 |
# Load background image
|
31 |
try:
|
32 |
-
bg = Image.open("bg.png").convert("
|
|
|
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)
|
@@ -67,6 +72,7 @@ def generate_profile():
|
|
67 |
draw_mask.ellipse((0, 0, pfp_size, pfp_size), fill=255)
|
68 |
|
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.")
|
@@ -76,7 +82,7 @@ def generate_profile():
|
|
76 |
# Define text positions
|
77 |
y_start = pfp_y + pfp_size + 30
|
78 |
spacing = 80
|
79 |
-
text_color = (
|
80 |
|
81 |
# Function to center text
|
82 |
def draw_centered_text(text, y, font):
|
@@ -90,20 +96,25 @@ def generate_profile():
|
|
90 |
draw.text((x, y), text, font=font, fill=text_color)
|
91 |
|
92 |
# Draw text
|
|
|
93 |
draw_centered_text(f"Name: {name}", y_start, font_large)
|
94 |
draw_centered_text(f"Rank: {rank}", y_start + spacing, font_small)
|
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__':
|
|
|
12 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
13 |
|
14 |
def load_font(font_path, size):
|
15 |
+
"""Load a font or fallback to default Arial."""
|
16 |
try:
|
17 |
return ImageFont.truetype(font_path, size)
|
18 |
except IOError:
|
|
|
30 |
|
31 |
# Load background image
|
32 |
try:
|
33 |
+
bg = Image.open("bg.png").convert("RGBA") # Ensure it supports transparency
|
34 |
+
print("Background image loaded successfully.")
|
35 |
except IOError:
|
36 |
return "Error: Background image 'bg.png' not found.", 500
|
37 |
|
38 |
draw = ImageDraw.Draw(bg)
|
39 |
W, H = bg.size # Get background dimensions
|
40 |
+
print(f"Canvas Size: {W}x{H}")
|
41 |
|
42 |
# Load fonts
|
43 |
font_large = load_font("font.ttf", 50)
|
44 |
font_small = load_font("font.ttf", 40)
|
45 |
|
|
|
|
|
|
|
46 |
# Profile picture settings
|
47 |
pfp_size = 150
|
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")
|
51 |
|
52 |
+
# Load and save profile picture
|
53 |
try:
|
54 |
if pfp_url:
|
55 |
response = requests.get(pfp_url, timeout=5)
|
56 |
if response.status_code == 200:
|
57 |
pfp = Image.open(BytesIO(response.content)).convert("RGBA")
|
58 |
+
print("Profile picture downloaded successfully.")
|
59 |
else:
|
60 |
raise ValueError("Invalid profile picture URL")
|
61 |
else:
|
62 |
pfp = Image.open("fallback.png").convert("RGBA")
|
63 |
+
print("Using fallback profile picture.")
|
64 |
|
65 |
pfp = pfp.resize((pfp_size, pfp_size), Image.LANCZOS)
|
66 |
+
pfp.save(pfp_path) # Save before using
|
67 |
+
print(f"Profile picture saved at {pfp_path}")
|
68 |
|
69 |
# Create circular mask
|
70 |
mask = Image.new("L", (pfp_size, pfp_size), 0)
|
|
|
72 |
draw_mask.ellipse((0, 0, pfp_size, pfp_size), fill=255)
|
73 |
|
74 |
# Apply mask and paste profile picture
|
75 |
+
pfp = Image.open(pfp_path).convert("RGBA") # Reload from saved file
|
76 |
pfp = Image.composite(pfp, Image.new("RGBA", pfp.size, (0, 0, 0, 0)), mask)
|
77 |
bg.paste(pfp, (pfp_x, pfp_y), pfp)
|
78 |
print("Profile picture successfully added.")
|
|
|
82 |
# Define text positions
|
83 |
y_start = pfp_y + pfp_size + 30
|
84 |
spacing = 80
|
85 |
+
text_color = (255, 255, 255) # White for visibility
|
86 |
|
87 |
# Function to center text
|
88 |
def draw_centered_text(text, y, font):
|
|
|
96 |
draw.text((x, y), text, font=font, fill=text_color)
|
97 |
|
98 |
# Draw text
|
99 |
+
print("Drawing text...")
|
100 |
draw_centered_text(f"Name: {name}", y_start, font_large)
|
101 |
draw_centered_text(f"Rank: {rank}", y_start + spacing, font_small)
|
102 |
draw_centered_text(f"Balance: {balance}", y_start + 2 * spacing, font_small)
|
103 |
draw_centered_text(f"Next Rank: {next_rank}", y_start + 3 * spacing, font_small)
|
|
|
104 |
print("Text successfully drawn on image.")
|
105 |
|
106 |
+
# Save final image
|
107 |
output_path = os.path.join(TEMP_DIR, f"profile_{uuid.uuid4().hex}.png")
|
108 |
bg.save(output_path)
|
109 |
+
print(f"Final image saved at {output_path}")
|
110 |
|
111 |
# Send and delete image
|
112 |
response = send_file(output_path, mimetype='image/png')
|
113 |
+
|
114 |
+
# Cleanup saved files
|
115 |
os.remove(output_path)
|
116 |
+
os.remove(pfp_path)
|
117 |
+
|
118 |
return response
|
119 |
|
120 |
if __name__ == '__main__':
|