ArrcttacsrjksX commited on
Commit
65c7b42
·
verified ·
1 Parent(s): 38c5500

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -25
app.py CHANGED
@@ -5,6 +5,7 @@ from PIL import Image, ImageDraw, ImageFont
5
  import textwrap
6
  import os
7
  import matplotlib
 
8
 
9
  def get_system_fonts():
10
  fonts = []
@@ -19,32 +20,29 @@ def parse_color(color):
19
  return tuple(int(float(c.strip())) for c in color[:3])
20
  return color
21
 
22
- def render_plain_text_image(text, font_size, width, height, bg_color, text_color, font_name, align):
23
- bg_color = parse_color(bg_color)
24
- text_color = parse_color(text_color)
25
-
26
- img = Image.new("RGB", (width, height), color=bg_color)
27
- draw = ImageDraw.Draw(img)
28
-
29
- try:
30
- font_path = matplotlib.font_manager.findfont(font_name)
31
- font = ImageFont.truetype(font_path, font_size)
32
- except Exception:
33
- font = ImageFont.load_default()
34
-
35
- margin = 10
36
- max_width = width - 2 * margin
37
  lines = []
38
  for line in text.split('\n'):
39
- lines.extend(textwrap.wrap(line, width=int(max_width / font_size * 1.8)))
40
-
41
  bbox = font.getbbox('Ay')
42
  line_height = bbox[3] - bbox[1]
43
- total_text_height = line_height * len(lines)
44
-
45
- y = (height - total_text_height) // 2
46
 
47
- for line in lines:
 
 
 
 
 
 
 
 
 
 
 
48
  bbox = font.getbbox(line)
49
  line_width = bbox[2] - bbox[0]
50
 
@@ -57,8 +55,49 @@ def render_plain_text_image(text, font_size, width, height, bg_color, text_color
57
 
58
  draw.text((x, y), line, fill=text_color, font=font)
59
  y += line_height
 
 
60
 
61
- return img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  def render_math_image(text, font_size, width, height, bg_color, text_color):
64
  bg_color = parse_color(bg_color)
@@ -89,7 +128,6 @@ def text_to_image(input_text, font_size, width, height, bg_color, text_color, mo
89
  else:
90
  return "Invalid mode selected!"
91
 
92
- # Return the PIL Image directly instead of BytesIO
93
  return img
94
 
95
  def handle_file_upload(file, font_size, width, height, bg_color, text_color, mode, font_name, align, image_format):
@@ -107,7 +145,7 @@ with gr.Blocks() as demo:
107
  gr.Markdown("# 🖼️ Text to Image Converter")
108
 
109
  with gr.Row():
110
- input_text = gr.Textbox(label="Enter Text", placeholder="Type or paste text here...")
111
  file_input = gr.File(label="Upload a Text File", type="filepath")
112
 
113
  with gr.Row():
@@ -117,7 +155,7 @@ with gr.Blocks() as demo:
117
 
118
  with gr.Row():
119
  width = gr.Slider(200, 2000, value=800, label="Image Width")
120
- height = gr.Slider(200, 2000, value=600, label="Image Height")
121
 
122
  with gr.Row():
123
  bg_color = gr.ColorPicker(label="Background Color", value="#FFFFFF")
 
5
  import textwrap
6
  import os
7
  import matplotlib
8
+ import math
9
 
10
  def get_system_fonts():
11
  fonts = []
 
20
  return tuple(int(float(c.strip())) for c in color[:3])
21
  return color
22
 
23
+ def calculate_text_dimensions(text, font, max_width, margin):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  lines = []
25
  for line in text.split('\n'):
26
+ lines.extend(textwrap.wrap(line, width=int(max_width / font.size * 1.8)))
27
+
28
  bbox = font.getbbox('Ay')
29
  line_height = bbox[3] - bbox[1]
30
+ total_height = line_height * len(lines)
31
+
32
+ return lines, line_height, total_height
33
 
34
+ def create_text_segment(lines, start_idx, max_lines, width, height, bg_color, text_color, font, align, margin):
35
+ img = Image.new("RGB", (width, height), color=bg_color)
36
+ draw = ImageDraw.Draw(img)
37
+
38
+ bbox = font.getbbox('Ay')
39
+ line_height = bbox[3] - bbox[1]
40
+
41
+ y = margin
42
+ end_idx = min(start_idx + max_lines, len(lines))
43
+ segment_lines = lines[start_idx:end_idx]
44
+
45
+ for line in segment_lines:
46
  bbox = font.getbbox(line)
47
  line_width = bbox[2] - bbox[0]
48
 
 
55
 
56
  draw.text((x, y), line, fill=text_color, font=font)
57
  y += line_height
58
+
59
+ return img, end_idx
60
 
61
+ def render_plain_text_image(text, font_size, width, height, bg_color, text_color, font_name, align):
62
+ bg_color = parse_color(bg_color)
63
+ text_color = parse_color(text_color)
64
+ margin = 10
65
+
66
+ try:
67
+ font_path = matplotlib.font_manager.findfont(font_name)
68
+ font = ImageFont.truetype(font_path, font_size)
69
+ except Exception:
70
+ font = ImageFont.load_default()
71
+
72
+ # Calculate dimensions
73
+ max_width = width - 2 * margin
74
+ lines, line_height, total_text_height = calculate_text_dimensions(text, font, max_width, margin)
75
+
76
+ # Calculate how many lines can fit in one segment
77
+ max_lines_per_segment = (height - 2 * margin) // line_height
78
+
79
+ # Calculate number of segments needed
80
+ num_segments = math.ceil(len(lines) / max_lines_per_segment)
81
+
82
+ # Create segments
83
+ segments = []
84
+ current_line = 0
85
+
86
+ for i in range(num_segments):
87
+ segment_img, current_line = create_text_segment(
88
+ lines, current_line, max_lines_per_segment,
89
+ width, height, bg_color, text_color, font, align, margin
90
+ )
91
+ segments.append(segment_img)
92
+
93
+ # Combine all segments vertically
94
+ total_height = len(segments) * height
95
+ final_image = Image.new("RGB", (width, total_height), color=bg_color)
96
+
97
+ for i, segment in enumerate(segments):
98
+ final_image.paste(segment, (0, i * height))
99
+
100
+ return final_image
101
 
102
  def render_math_image(text, font_size, width, height, bg_color, text_color):
103
  bg_color = parse_color(bg_color)
 
128
  else:
129
  return "Invalid mode selected!"
130
 
 
131
  return img
132
 
133
  def handle_file_upload(file, font_size, width, height, bg_color, text_color, mode, font_name, align, image_format):
 
145
  gr.Markdown("# 🖼️ Text to Image Converter")
146
 
147
  with gr.Row():
148
+ input_text = gr.Textbox(label="Enter Text", placeholder="Type or paste text here...", lines=5)
149
  file_input = gr.File(label="Upload a Text File", type="filepath")
150
 
151
  with gr.Row():
 
155
 
156
  with gr.Row():
157
  width = gr.Slider(200, 2000, value=800, label="Image Width")
158
+ height = gr.Slider(200, 2000, value=600, label="Base Height")
159
 
160
  with gr.Row():
161
  bg_color = gr.ColorPicker(label="Background Color", value="#FFFFFF")