Reaperxxxx commited on
Commit
d71cbc2
·
verified ·
1 Parent(s): 378af0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -11,6 +11,14 @@ app = Flask(__name__)
11
  TEMP_DIR = "temp"
12
  os.makedirs(TEMP_DIR, exist_ok=True)
13
 
 
 
 
 
 
 
 
 
14
  @app.route('/api')
15
  def generate_profile():
16
  # Get query parameters
@@ -21,12 +29,16 @@ def generate_profile():
21
  pfp_url = request.args.get('pfp', None) # Profile picture URL
22
 
23
  # Load background
24
- bg = Image.open("bg.png").convert("RGBA")
25
- draw = ImageDraw.Draw(bg)
 
 
26
 
27
- # Load font
28
- font_large = ImageFont.truetype("font.ttf", 50) # Larger font for name
29
- font_small = ImageFont.truetype("font.ttf", 40) # Smaller font for details
 
 
30
 
31
  # Get image size
32
  W, H = bg.size
@@ -64,9 +76,13 @@ def generate_profile():
64
 
65
  # Centered text positions
66
  def draw_centered_text(text, y, font):
67
- bbox = draw.textbbox((0, 0), text, font=font) # Get bounding box
68
- text_width = bbox[2] - bbox[0] # Calculate text width
69
- x = (W - text_width) // 2 # Center the text
 
 
 
 
70
  draw.text((x, y), text, font=font, fill=text_color)
71
 
72
  # Draw text
 
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) # Fallback to Arial
21
+
22
  @app.route('/api')
23
  def generate_profile():
24
  # Get query parameters
 
29
  pfp_url = request.args.get('pfp', None) # Profile picture URL
30
 
31
  # Load background
32
+ try:
33
+ bg = Image.open("bg.png").convert("RGB") # Convert to RGB to ensure text visibility
34
+ except IOError:
35
+ return "Error: Background image 'bg.png' not found.", 500
36
 
37
+ draw = ImageDraw.Draw(bg)
38
+
39
+ # Load fonts safely
40
+ font_large = load_font("font.ttf", 50)
41
+ font_small = load_font("font.ttf", 40)
42
 
43
  # Get image size
44
  W, H = bg.size
 
76
 
77
  # Centered text positions
78
  def draw_centered_text(text, y, font):
79
+ try:
80
+ bbox = draw.textbbox((0, 0), text, font=font) # Works in new Pillow versions
81
+ text_width = bbox[2] - bbox[0]
82
+ except AttributeError:
83
+ text_width, _ = draw.textsize(text, font=font) # Fallback for older versions
84
+
85
+ x = (W - text_width) // 2
86
  draw.text((x, y), text, font=font, fill=text_color)
87
 
88
  # Draw text