VirtualOasis commited on
Commit
6418207
·
verified ·
1 Parent(s): cf9170b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -25,14 +25,16 @@ def draw_lattice_grid(draw, width, height, spacing=100, color=(235, 235, 235)):
25
  draw.line([(0, y), (width, y)], fill=color, width=2)
26
 
27
 
28
- def text_to_images_and_base64_generator(text_content, style='lines'):
29
  """
30
  Converts text into images, returning both PIL objects for UI preview
31
- and base64 strings for an API/MCP client.
32
 
33
  Args:
34
  text_content (str): The text to be converted.
35
- style (str, optional): The background style ('plain', 'lines', 'dots', 'grid').
 
 
36
 
37
  Returns:
38
  tuple: A tuple containing (list of PIL.Image.Image, list of base64 strings).
@@ -41,16 +43,23 @@ def text_to_images_and_base64_generator(text_content, style='lines'):
41
  gr.Warning("Input text is empty. Please enter some text to generate images.")
42
  return [], []
43
 
 
 
 
 
 
 
 
 
 
44
  # --- Configuration ---
45
  IMG_WIDTH = 1080
46
  IMG_HEIGHT = 1080
47
- BACKGROUND_COLOR = (255, 255, 255)
48
  TEXT_COLOR = (10, 10, 10)
49
  STYLE_COLOR = (225, 225, 225)
50
 
51
  PADDING_X = 80
52
  PADDING_Y = 80
53
- FONT_SIZE = 48
54
  LINE_SPACING = 20
55
 
56
  # --- Font Loading ---
@@ -64,12 +73,12 @@ def text_to_images_and_base64_generator(text_content, style='lines'):
64
  ]
65
  for f_path in font_paths_to_try:
66
  try:
67
- font = ImageFont.truetype(f_path, FONT_SIZE)
68
  break
69
  except IOError:
70
  continue
71
  if not font:
72
- font = ImageFont.load_default()
73
  except Exception as e:
74
  print(f"Font loading error: {e}")
75
  font = ImageFont.load_default()
@@ -112,8 +121,10 @@ def text_to_images_and_base64_generator(text_content, style='lines'):
112
  try:
113
  line_height = font.getbbox("A")[3] - font.getbbox("A")[1]
114
  except AttributeError:
115
- line_height = 12
116
-
 
 
117
  PARAGRAPH_SPACING = line_height
118
 
119
  def create_and_encode_page(content):
@@ -123,7 +134,7 @@ def text_to_images_and_base64_generator(text_content, style='lines'):
123
 
124
  if style == 'lines':
125
  line_style_spacing = line_height + LINE_SPACING
126
- draw_horizontal_lines(draw, IMG_WIDTH, IMG_HEIGHT, spacing=line_style_spacing, color=STYLE_COLOR)
127
  elif style == 'dots':
128
  draw_dot_grid(draw, IMG_WIDTH, IMG_HEIGHT, color=STYLE_COLOR)
129
  elif style == 'grid':
@@ -137,10 +148,8 @@ def text_to_images_and_base64_generator(text_content, style='lines'):
137
  else:
138
  current_y += PARAGRAPH_SPACING
139
 
140
- # Store the PIL image object for the gallery
141
  generated_images.append(img)
142
 
143
- # Encode image to base64 string for the API
144
  buffered = io.BytesIO()
145
  img.save(buffered, format="PNG")
146
  img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
@@ -173,8 +182,10 @@ One day, a young girl with eyes as curious as a cat's wandered into his shop. Sh
173
  demo = gr.Interface(
174
  fn=text_to_images_and_base64_generator,
175
  inputs=[
176
- gr.Textbox(lines=15, label="Text Content", placeholder="Paste your long-form text here...", value=example_text),
177
- gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines')
 
 
178
  ],
179
  outputs=[
180
  gr.Gallery(label="Generated Images (Preview)", show_label=True, preview=True),
 
25
  draw.line([(0, y), (width, y)], fill=color, width=2)
26
 
27
 
28
+ def text_to_images_and_base64_generator(text_content, style, font_size, bg_color_name):
29
  """
30
  Converts text into images, returning both PIL objects for UI preview
31
+ and base64 strings for an API/MCP client, with customizable options.
32
 
33
  Args:
34
  text_content (str): The text to be converted.
35
+ style (str): The background style ('plain', 'lines', 'dots', 'grid').
36
+ font_size (int): The font size for the text.
37
+ bg_color_name (str): The name of the background color.
38
 
39
  Returns:
40
  tuple: A tuple containing (list of PIL.Image.Image, list of base64 strings).
 
43
  gr.Warning("Input text is empty. Please enter some text to generate images.")
44
  return [], []
45
 
46
+ # --- Color Mapping ---
47
+ color_map = {
48
+ "White": (255, 255, 255),
49
+ "Light Gray": (240, 240, 240),
50
+ "Beige": (245, 245, 220),
51
+ "Pale Blue": (220, 230, 245)
52
+ }
53
+ BACKGROUND_COLOR = color_map.get(bg_color_name, (255, 255, 255))
54
+
55
  # --- Configuration ---
56
  IMG_WIDTH = 1080
57
  IMG_HEIGHT = 1080
 
58
  TEXT_COLOR = (10, 10, 10)
59
  STYLE_COLOR = (225, 225, 225)
60
 
61
  PADDING_X = 80
62
  PADDING_Y = 80
 
63
  LINE_SPACING = 20
64
 
65
  # --- Font Loading ---
 
73
  ]
74
  for f_path in font_paths_to_try:
75
  try:
76
+ font = ImageFont.truetype(f_path, font_size)
77
  break
78
  except IOError:
79
  continue
80
  if not font:
81
+ font = ImageFont.load_default() # Note: Default font doesn't support size
82
  except Exception as e:
83
  print(f"Font loading error: {e}")
84
  font = ImageFont.load_default()
 
121
  try:
122
  line_height = font.getbbox("A")[3] - font.getbbox("A")[1]
123
  except AttributeError:
124
+ # Fallback for default font which has no getbbox
125
+ bbox = font.getmask("A").getbbox()
126
+ line_height = bbox[3] - bbox[1] if bbox else 12
127
+
128
  PARAGRAPH_SPACING = line_height
129
 
130
  def create_and_encode_page(content):
 
134
 
135
  if style == 'lines':
136
  line_style_spacing = line_height + LINE_SPACING
137
+ draw_horizontal_lines(draw, IMG_WIDTH, IMG_HEIGHT, spacing=int(line_style_spacing), color=STYLE_COLOR)
138
  elif style == 'dots':
139
  draw_dot_grid(draw, IMG_WIDTH, IMG_HEIGHT, color=STYLE_COLOR)
140
  elif style == 'grid':
 
148
  else:
149
  current_y += PARAGRAPH_SPACING
150
 
 
151
  generated_images.append(img)
152
 
 
153
  buffered = io.BytesIO()
154
  img.save(buffered, format="PNG")
155
  img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
 
182
  demo = gr.Interface(
183
  fn=text_to_images_and_base64_generator,
184
  inputs=[
185
+ gr.Textbox(lines=10, label="Text Content", placeholder="Paste your long-form text here...", value=example_text),
186
+ gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines'),
187
+ gr.Slider(24, 72, value=48, step=2, label="Font Size"),
188
+ gr.Radio(["White", "Light Gray", "Beige", "Pale Blue"], label="Background Color", value="White")
189
  ],
190
  outputs=[
191
  gr.Gallery(label="Generated Images (Preview)", show_label=True, preview=True),