ArrcttacsrjksX commited on
Commit
a5c1cbd
·
verified ·
1 Parent(s): 5498ddc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -46,7 +46,7 @@ def calculate_text_dimensions(text, font, max_width, margin):
46
 
47
  return lines, line_height, total_height
48
 
49
- def create_text_segment(lines, start_idx, max_lines, width, height, bg_color, text_color, font, align, margin):
50
  img = Image.new("RGB", (width, height), color=bg_color)
51
  draw = ImageDraw.Draw(img)
52
 
@@ -69,11 +69,11 @@ def create_text_segment(lines, start_idx, max_lines, width, height, bg_color, te
69
  x = width - line_width - margin
70
 
71
  draw.text((x, y), line, fill=text_color, font=font)
72
- y += line_height
73
 
74
  return img, end_idx
75
 
76
- def render_plain_text_image(text, font_size, width, height, bg_color, text_color, font_path, align):
77
  bg_color = parse_color(bg_color)
78
  text_color = parse_color(text_color)
79
  margin = 10
@@ -88,7 +88,7 @@ def render_plain_text_image(text, font_size, width, height, bg_color, text_color
88
  lines, line_height, total_text_height = calculate_text_dimensions(text, font, max_width, margin)
89
 
90
  # Calculate how many lines can fit in one segment
91
- max_lines_per_segment = (height - 2 * margin) // line_height
92
 
93
  # Calculate number of segments needed
94
  num_segments = math.ceil(len(lines) / max_lines_per_segment)
@@ -100,7 +100,7 @@ def render_plain_text_image(text, font_size, width, height, bg_color, text_color
100
  for i in range(num_segments):
101
  segment_img, current_line = create_text_segment(
102
  lines, current_line, max_lines_per_segment,
103
- width, height, bg_color, text_color, font, align, margin
104
  )
105
  segments.append(segment_img)
106
 
@@ -134,9 +134,9 @@ def render_math_image(text, font_size, width, height, bg_color, text_color):
134
  img = Image.open(buf)
135
  return img
136
 
137
- def text_to_image(input_text, font_size, width, height, bg_color, text_color, mode, font_path, align, image_format):
138
  if mode == "Plain Text":
139
- img = render_plain_text_image(input_text, font_size, width, height, bg_color, text_color, font_path, align)
140
  elif mode == "LaTeX Math":
141
  img = render_math_image(input_text, font_size, width, height, bg_color, text_color)
142
  else:
@@ -173,6 +173,9 @@ with gr.Blocks() as demo:
173
  mode = gr.Radio(["Plain Text", "LaTeX Math"], label="Rendering Mode", value="Plain Text")
174
  image_format = gr.Radio(["PNG", "JPEG"], label="Image Format", value="PNG")
175
 
 
 
 
176
  output_image = gr.Image(label="Generated Image")
177
 
178
  with gr.Row():
@@ -183,7 +186,7 @@ with gr.Blocks() as demo:
183
  text_to_image,
184
  inputs=[
185
  input_text, font_size, width, height, bg_color, text_color,
186
- mode, font_name, align, image_format
187
  ],
188
  outputs=output_image
189
  )
 
46
 
47
  return lines, line_height, total_height
48
 
49
+ def create_text_segment(lines, start_idx, max_lines, width, height, bg_color, text_color, font, align, margin, line_spacing):
50
  img = Image.new("RGB", (width, height), color=bg_color)
51
  draw = ImageDraw.Draw(img)
52
 
 
69
  x = width - line_width - margin
70
 
71
  draw.text((x, y), line, fill=text_color, font=font)
72
+ y += line_height * line_spacing # Adjust line spacing here
73
 
74
  return img, end_idx
75
 
76
+ def render_plain_text_image(text, font_size, width, height, bg_color, text_color, font_path, align, line_spacing):
77
  bg_color = parse_color(bg_color)
78
  text_color = parse_color(text_color)
79
  margin = 10
 
88
  lines, line_height, total_text_height = calculate_text_dimensions(text, font, max_width, margin)
89
 
90
  # Calculate how many lines can fit in one segment
91
+ max_lines_per_segment = (height - 2 * margin) // (line_height * line_spacing)
92
 
93
  # Calculate number of segments needed
94
  num_segments = math.ceil(len(lines) / max_lines_per_segment)
 
100
  for i in range(num_segments):
101
  segment_img, current_line = create_text_segment(
102
  lines, current_line, max_lines_per_segment,
103
+ width, height, bg_color, text_color, font, align, margin, line_spacing
104
  )
105
  segments.append(segment_img)
106
 
 
134
  img = Image.open(buf)
135
  return img
136
 
137
+ def text_to_image(input_text, font_size, width, height, bg_color, text_color, mode, font_path, align, line_spacing, image_format):
138
  if mode == "Plain Text":
139
+ img = render_plain_text_image(input_text, font_size, width, height, bg_color, text_color, font_path, align, line_spacing)
140
  elif mode == "LaTeX Math":
141
  img = render_math_image(input_text, font_size, width, height, bg_color, text_color)
142
  else:
 
173
  mode = gr.Radio(["Plain Text", "LaTeX Math"], label="Rendering Mode", value="Plain Text")
174
  image_format = gr.Radio(["PNG", "JPEG"], label="Image Format", value="PNG")
175
 
176
+ # Add line spacing slider
177
+ line_spacing = gr.Slider(1.0, 3.0, value=1.2, step=0.1, label="Line Spacing")
178
+
179
  output_image = gr.Image(label="Generated Image")
180
 
181
  with gr.Row():
 
186
  text_to_image,
187
  inputs=[
188
  input_text, font_size, width, height, bg_color, text_color,
189
+ mode, font_name, align, line_spacing, image_format
190
  ],
191
  outputs=output_image
192
  )