bgamazay commited on
Commit
4c62121
·
verified ·
1 Parent(s): fe4fae8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -99,29 +99,35 @@ def main():
99
 
100
  def create_label_single_pass(background_image, model_data, final_size=(260, 364)):
101
  """
102
- Resizes the background to 260×364, then draws text onto it.
103
  """
104
- # 1. Resize background to final_size
105
- bg_resized = background_image.resize(final_size, Image.Resampling.LANCZOS)
106
- draw = ImageDraw.Draw(bg_resized)
107
 
108
- # 2. Load fonts at sizes appropriate for a 260×364 label
 
 
 
 
 
 
109
  try:
110
- title_font = ImageFont.truetype("Inter_24pt-Bold.ttf", size=12)
111
- details_font = ImageFont.truetype("Inter_18pt-Regular.ttf", size=10)
112
- energy_font = ImageFont.truetype("Inter_18pt-Medium.ttf", size=12)
113
  except Exception as e:
114
  st.error(f"Font loading failed: {e}")
115
- return bg_resized
116
 
117
- # 3. Place your text.
118
- title_x, title_y = 16, 75
119
- details_x, details_y = 245, 130
120
- energy_x, energy_y = 245, 250
121
 
122
  # Text 1 (title)
123
  draw.text((title_x, title_y), str(model_data['model']), font=title_font, fill="black")
124
- draw.text((title_x, title_y + 20), str(model_data['provider']), font=title_font, fill="black")
125
 
126
  # Text 2 (details)
127
  details_lines = [
@@ -133,7 +139,7 @@ def create_label_single_pass(background_image, model_data, final_size=(260, 364)
133
  bbox = draw.textbbox((0, 0), line, font=details_font)
134
  text_width = bbox[2] - bbox[0]
135
  # Right-justify the details text at details_x
136
- draw.text((details_x - text_width, details_y + i * 25), line, font=details_font, fill="black")
137
 
138
  # Text 3 (energy)
139
  energy_text = str(model_data['energy'])
@@ -142,8 +148,9 @@ def create_label_single_pass(background_image, model_data, final_size=(260, 364)
142
  # Right-align the energy text at energy_x
143
  draw.text((energy_x - energy_text_width, energy_y), energy_text, font=energy_font, fill="black")
144
 
145
- return bg_resized
146
-
 
147
 
148
  if __name__ == "__main__":
149
  main()
 
99
 
100
  def create_label_single_pass(background_image, model_data, final_size=(260, 364)):
101
  """
102
+ Resizes the background to 260×364 with sharper output and draws text onto it.
103
  """
104
+ # High-resolution canvas scaling factor (e.g., 4x)
105
+ scale_factor = 4
106
+ high_res_size = (final_size[0] * scale_factor, final_size[1] * scale_factor)
107
 
108
+ # Step 1: Resize background to high-resolution canvas
109
+ high_res_image = background_image.resize(high_res_size, Image.Resampling.LANCZOS)
110
+
111
+ # Step 2: Draw text on the high-resolution image
112
+ draw = ImageDraw.Draw(high_res_image)
113
+
114
+ # Load fonts with scaled sizes
115
  try:
116
+ title_font = ImageFont.truetype("Inter_24pt-Bold.ttf", size=12 * scale_factor)
117
+ details_font = ImageFont.truetype("Inter_18pt-Regular.ttf", size=10 * scale_factor)
118
+ energy_font = ImageFont.truetype("Inter_18pt-Medium.ttf", size=12 * scale_factor)
119
  except Exception as e:
120
  st.error(f"Font loading failed: {e}")
121
+ return high_res_image
122
 
123
+ # Scaled positions for text
124
+ title_x, title_y = 16 * scale_factor, 75 * scale_factor
125
+ details_x, details_y = 245 * scale_factor, 130 * scale_factor
126
+ energy_x, energy_y = 245 * scale_factor, 250 * scale_factor
127
 
128
  # Text 1 (title)
129
  draw.text((title_x, title_y), str(model_data['model']), font=title_font, fill="black")
130
+ draw.text((title_x, title_y + 20 * scale_factor), str(model_data['provider']), font=title_font, fill="black")
131
 
132
  # Text 2 (details)
133
  details_lines = [
 
139
  bbox = draw.textbbox((0, 0), line, font=details_font)
140
  text_width = bbox[2] - bbox[0]
141
  # Right-justify the details text at details_x
142
+ draw.text((details_x - text_width, details_y + i * 25 * scale_factor), line, font=details_font, fill="black")
143
 
144
  # Text 3 (energy)
145
  energy_text = str(model_data['energy'])
 
148
  # Right-align the energy text at energy_x
149
  draw.text((energy_x - energy_text_width, energy_y), energy_text, font=energy_font, fill="black")
150
 
151
+ # Step 3: Downscale to final size
152
+ final_image = high_res_image.resize(final_size, Image.Resampling.LANCZOS)
153
+ return final_image
154
 
155
  if __name__ == "__main__":
156
  main()