Reaperxxxx commited on
Commit
3de671d
·
verified ·
1 Parent(s): 571fa40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -34
app.py CHANGED
@@ -16,7 +16,6 @@ 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')
@@ -31,37 +30,33 @@ def generate_profile():
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
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", 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")
52
 
53
- # Load and save profile picture
54
  try:
55
  if pfp_url:
56
  response = requests.get(pfp_url, timeout=5)
57
  if response.status_code == 200:
58
  pfp = Image.open(BytesIO(response.content)).convert("RGBA")
59
- print("Profile picture downloaded successfully.")
60
  else:
61
  raise ValueError("Invalid profile picture URL")
62
  else:
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)
@@ -81,43 +76,39 @@ def generate_profile():
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):
95
- try:
96
- bbox = draw.textbbox((0, 0), text, font=font)
97
- text_width = bbox[2] - bbox[0]
98
- except AttributeError:
99
- text_width, _ = draw.textsize(text, font=font)
100
-
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
113
  output_path = os.path.join(TEMP_DIR, f"profile_{uuid.uuid4().hex}.png")
114
  bg.save(output_path)
115
- print(f"Final image saved at {output_path}")
116
 
117
  # Send and delete image
118
  response = send_file(output_path, mimetype='image/png')
119
 
120
- # Cleanup saved files safely
121
  if os.path.exists(output_path):
122
  os.remove(output_path)
123
  if os.path.exists(pfp_path):
 
16
  try:
17
  return ImageFont.truetype(font_path, size)
18
  except IOError:
 
19
  return ImageFont.truetype("arial.ttf", size)
20
 
21
  @app.route('/api')
 
30
  # Load background image
31
  try:
32
  bg = Image.open("bg.png").convert("RGBA") # Ensure transparency support
 
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", 45) # Name
41
+ font_medium = load_font("font.ttf", 35) # Rank, Balance
42
+ font_small = load_font("font.ttf", 30) # Next Rank
43
 
44
  # Profile picture settings
45
  pfp_size = 170
46
+ pfp_x = 50 # Align profile picture to the left
47
  pfp_y = 50
 
48
  pfp_path = os.path.join(TEMP_DIR, f"pfp_{uuid.uuid4().hex}.png")
49
 
50
+ # Load profile picture
51
  try:
52
  if pfp_url:
53
  response = requests.get(pfp_url, timeout=5)
54
  if response.status_code == 200:
55
  pfp = Image.open(BytesIO(response.content)).convert("RGBA")
 
56
  else:
57
  raise ValueError("Invalid profile picture URL")
58
  else:
59
  pfp = Image.open("fallback.png").convert("RGBA")
 
60
 
61
  # Resize profile picture
62
  pfp = pfp.resize((pfp_size, pfp_size), Image.LANCZOS)
 
76
 
77
  # Paste profile picture with border
78
  bg.paste(border, (pfp_x - border_size // 2, pfp_y - border_size // 2), border_mask)
 
79
  except Exception as e:
80
  print(f"Error loading profile picture: {e}")
81
 
82
  # Define text positions
83
+ text_x = pfp_x + pfp_size + 30 # Start text next to profile picture
84
+ text_y_start = pfp_y + 30
85
  text_spacing = 60
86
+ text_color = (255, 255, 255)
87
+
88
+ # Function to handle text resizing for long names
89
+ def fit_text(text, max_width, base_font):
90
+ font = base_font
91
+ while draw.textlength(text, font=font) > max_width and font.size > 20:
92
+ font = load_font("font.ttf", font.size - 2)
93
+ return font
94
+
95
+ # Adjust name font size dynamically
96
+ name_font = fit_text(name, W - text_x - 50, font_large)
97
+
98
+ # Draw aligned text
99
+ draw.text((text_x, text_y_start), name, font=name_font, fill=text_color)
100
+ draw.text((text_x, text_y_start + text_spacing), f"Rank: {rank}", font=font_medium, fill=text_color)
101
+ draw.text((text_x, text_y_start + 2 * text_spacing), f"Balance: {balance}", font=font_medium, fill=text_color)
102
+ draw.text((text_x, text_y_start + 3 * text_spacing), f"Next Rank: {next_rank}", font=font_small, fill=text_color)
 
 
 
103
 
104
  # Save final image
105
  output_path = os.path.join(TEMP_DIR, f"profile_{uuid.uuid4().hex}.png")
106
  bg.save(output_path)
 
107
 
108
  # Send and delete image
109
  response = send_file(output_path, mimetype='image/png')
110
 
111
+ # Cleanup saved files
112
  if os.path.exists(output_path):
113
  os.remove(output_path)
114
  if os.path.exists(pfp_path):