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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -30
app.py CHANGED
@@ -7,12 +7,12 @@ def main():
7
  # Sidebar logo and title
8
  with st.sidebar:
9
  col1, col2 = st.columns([1, 5]) # Shrink the logo column and expand the text column
10
-
11
  with col1:
12
  logo = Image.open("logo.png")
13
  resized_logo = logo.resize((40, 40)) # Resize the logo
14
  st.image(resized_logo)
15
-
16
  with col2:
17
  st.markdown(
18
  """
@@ -77,11 +77,11 @@ def main():
77
  st.sidebar.error(f"Invalid score '{model_data['score']}'. Score must be an integer.")
78
  return
79
 
80
- # Keep the final label size at 260x364
81
- final_size = (260, 364)
82
  generated_label = create_label_single_pass(background, model_data, final_size)
83
 
84
- st.image(generated_label, caption="Generated Label Preview", width=final_size[0])
85
 
86
  img_buffer = io.BytesIO()
87
  generated_label.save(img_buffer, format="PNG")
@@ -97,37 +97,33 @@ def main():
97
  st.sidebar.write("#### 3. Share your label in technical reports, announcements, etc.")
98
 
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,7 +135,7 @@ def create_label_single_pass(background_image, model_data, final_size=(260, 364)
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,9 +144,8 @@ def create_label_single_pass(background_image, model_data, final_size=(260, 364)
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()
 
7
  # Sidebar logo and title
8
  with st.sidebar:
9
  col1, col2 = st.columns([1, 5]) # Shrink the logo column and expand the text column
10
+
11
  with col1:
12
  logo = Image.open("logo.png")
13
  resized_logo = logo.resize((40, 40)) # Resize the logo
14
  st.image(resized_logo)
15
+
16
  with col2:
17
  st.markdown(
18
  """
 
77
  st.sidebar.error(f"Invalid score '{model_data['score']}'. Score must be an integer.")
78
  return
79
 
80
+ # Keep the final label size at 520×728
81
+ final_size = (520, 728)
82
  generated_label = create_label_single_pass(background, model_data, final_size)
83
 
84
+ st.image(generated_label, caption="Generated Label Preview", width=520)
85
 
86
  img_buffer = io.BytesIO()
87
  generated_label.save(img_buffer, format="PNG")
 
97
  st.sidebar.write("#### 3. Share your label in technical reports, announcements, etc.")
98
 
99
 
100
+ def create_label_single_pass(background_image, model_data, final_size=(520, 728)):
101
  """
102
+ Resizes the background to 520×728, 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 520×728 label
 
 
 
109
  try:
110
+ title_font = ImageFont.truetype("Inter_24pt-Bold.ttf", size=27)
111
+ details_font = ImageFont.truetype("Inter_18pt-Regular.ttf", size=23)
112
+ energy_font = ImageFont.truetype("Inter_18pt-Medium.ttf", size=24)
113
  except Exception as e:
114
  st.error(f"Font loading failed: {e}")
115
+ return bg_resized
116
 
117
+ # 3. Place your text.
118
+ # You may need to experiment with x/y coordinates or font sizes
119
+ # to make it look right in 520×728.
120
+ title_x, title_y = 33, 150
121
+ details_x, details_y = 480, 256
122
+ energy_x, energy_y = 480, 472
123
 
124
  # Text 1 (title)
125
  draw.text((title_x, title_y), str(model_data['model']), font=title_font, fill="black")
126
+ draw.text((title_x, title_y + 38), str(model_data['provider']), font=title_font, fill="black")
127
 
128
  # Text 2 (details)
129
  details_lines = [
 
135
  bbox = draw.textbbox((0, 0), line, font=details_font)
136
  text_width = bbox[2] - bbox[0]
137
  # Right-justify the details text at details_x
138
+ draw.text((details_x - text_width, details_y + i*47), line, font=details_font, fill="black")
139
 
140
  # Text 3 (energy)
141
  energy_text = str(model_data['energy'])
 
144
  # Right-align the energy text at energy_x
145
  draw.text((energy_x - energy_text_width, energy_y), energy_text, font=energy_font, fill="black")
146
 
147
+ return bg_resized
148
+
 
149
 
150
  if __name__ == "__main__":
151
  main()