VirtualOasis commited on
Commit
3c7dd24
·
verified ·
1 Parent(s): baaef04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -25,7 +25,7 @@ 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, font_size, bg_color_name, aspect_ratio_str):
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.
@@ -36,6 +36,7 @@ def text_to_images_and_base64_generator(text_content, style, font_size, bg_color
36
  font_size (int): The font size for the text.
37
  bg_color_name (str): The name of the background color.
38
  aspect_ratio_str (str): The string representing the desired aspect ratio.
 
39
 
40
  Returns:
41
  tuple: A tuple containing (list of PIL.Image.Image, list of base64 strings).
@@ -44,11 +45,12 @@ def text_to_images_and_base64_generator(text_content, style, font_size, bg_color
44
  gr.Warning("Input text is empty. Please enter some text to generate images.")
45
  return [], []
46
 
47
- # --- Aspect Ratio and Color Mapping ---
48
  aspect_ratio_map = {
49
  "1:1 (Square)": (1080, 1080),
50
  "16:9 (Widescreen)": (1080, 608),
51
  "9:16 (Vertical)": (1080, 1920),
 
52
  "4:3 (Standard)": (1080, 810)
53
  }
54
  color_map = {
@@ -57,6 +59,14 @@ def text_to_images_and_base64_generator(text_content, style, font_size, bg_color
57
  "Beige": (245, 245, 220),
58
  "Pale Blue": (220, 230, 245)
59
  }
 
 
 
 
 
 
 
 
60
  IMG_WIDTH, IMG_HEIGHT = aspect_ratio_map.get(aspect_ratio_str, (1080, 1080))
61
  BACKGROUND_COLOR = color_map.get(bg_color_name, (255, 255, 255))
62
 
@@ -69,24 +79,25 @@ def text_to_images_and_base64_generator(text_content, style, font_size, bg_color
69
 
70
  # --- Font Loading ---
71
  font = None
72
- try:
73
- font_paths_to_try = [
74
- "Arial.ttf", "arial.ttf", "DejaVuSans.ttf",
75
- "/System/Library/Fonts/Supplemental/Arial.ttf",
76
- "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
77
- "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
78
- ]
79
- for f_path in font_paths_to_try:
80
- try:
81
- font = ImageFont.truetype(f_path, font_size)
82
- break
83
- except IOError:
84
- continue
85
- if not font:
86
- font = ImageFont.load_default() # Note: Default font doesn't support size
87
- except Exception as e:
88
- print(f"Font loading error: {e}")
89
  font = ImageFont.load_default()
 
90
 
91
  # --- Text Wrapping ---
92
  drawable_width = IMG_WIDTH - 2 * PADDING_X
@@ -126,7 +137,6 @@ def text_to_images_and_base64_generator(text_content, style, font_size, bg_color
126
  try:
127
  line_height = font.getbbox("A")[3] - font.getbbox("A")[1]
128
  except AttributeError:
129
- # Fallback for default font which has no getbbox
130
  bbox = font.getmask("A").getbbox()
131
  line_height = bbox[3] - bbox[1] if bbox else 12
132
 
@@ -191,7 +201,8 @@ demo = gr.Interface(
191
  gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines'),
192
  gr.Slider(24, 72, value=48, step=2, label="Font Size"),
193
  gr.Radio(["White", "Light Gray", "Beige", "Pale Blue"], label="Background Color", value="White"),
194
- gr.Radio(["1:1 (Square)", "16:9 (Widescreen)", "9:16 (Vertical)", "4:3 (Standard)"], label="Aspect Ratio", value="1:1 (Square)")
 
195
  ],
196
  outputs=[
197
  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, aspect_ratio_str, font_choice):
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.
 
36
  font_size (int): The font size for the text.
37
  bg_color_name (str): The name of the background color.
38
  aspect_ratio_str (str): The string representing the desired aspect ratio.
39
+ font_choice (str): The name of the font family to use.
40
 
41
  Returns:
42
  tuple: A tuple containing (list of PIL.Image.Image, list of base64 strings).
 
45
  gr.Warning("Input text is empty. Please enter some text to generate images.")
46
  return [], []
47
 
48
+ # --- Aspect Ratio, Color, and Font Mapping ---
49
  aspect_ratio_map = {
50
  "1:1 (Square)": (1080, 1080),
51
  "16:9 (Widescreen)": (1080, 608),
52
  "9:16 (Vertical)": (1080, 1920),
53
+ "3:4 (Standard)": (1080, 1440),
54
  "4:3 (Standard)": (1080, 810)
55
  }
56
  color_map = {
 
59
  "Beige": (245, 245, 220),
60
  "Pale Blue": (220, 230, 245)
61
  }
62
+ font_map = {
63
+ "Arial": ["arial.ttf", "Arial.ttf"],
64
+ "Times New Roman": ["times.ttf", "Times New Roman.ttf"],
65
+ "Courier New": ["cour.ttf", "Courier New.ttf"],
66
+ "Verdana": ["verdana.ttf", "Verdana.ttf"],
67
+ "Georgia": ["georgia.ttf", "Georgia.ttf"],
68
+ "DejaVu Sans": ["DejaVuSans.ttf"] # Good fallback
69
+ }
70
  IMG_WIDTH, IMG_HEIGHT = aspect_ratio_map.get(aspect_ratio_str, (1080, 1080))
71
  BACKGROUND_COLOR = color_map.get(bg_color_name, (255, 255, 255))
72
 
 
79
 
80
  # --- Font Loading ---
81
  font = None
82
+ selected_font_files = font_map.get(font_choice, [])
83
+
84
+ font_paths_to_try = selected_font_files + [
85
+ "Arial.ttf", "arial.ttf", "DejaVuSans.ttf",
86
+ "/System/Library/Fonts/Supplemental/Arial.ttf",
87
+ "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
88
+ "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
89
+ ]
90
+
91
+ for f_path in font_paths_to_try:
92
+ try:
93
+ font = ImageFont.truetype(f_path, font_size)
94
+ break
95
+ except IOError:
96
+ continue
97
+
98
+ if not font:
99
  font = ImageFont.load_default()
100
+ gr.Warning(f"Could not find '{font_choice}' or fallback fonts. Using default.")
101
 
102
  # --- Text Wrapping ---
103
  drawable_width = IMG_WIDTH - 2 * PADDING_X
 
137
  try:
138
  line_height = font.getbbox("A")[3] - font.getbbox("A")[1]
139
  except AttributeError:
 
140
  bbox = font.getmask("A").getbbox()
141
  line_height = bbox[3] - bbox[1] if bbox else 12
142
 
 
201
  gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines'),
202
  gr.Slider(24, 72, value=48, step=2, label="Font Size"),
203
  gr.Radio(["White", "Light Gray", "Beige", "Pale Blue"], label="Background Color", value="White"),
204
+ gr.Radio(["1:1 (Square)", "16:9 (Widescreen)", "9:16 (Vertical)", "4:3 (Standard)"], label="Aspect Ratio", value="1:1 (Square)"),
205
+ gr.Dropdown(["Arial", "Times New Roman", "Courier New", "Verdana", "Georgia", "DejaVu Sans"], label="Font Family", value="Arial")
206
  ],
207
  outputs=[
208
  gr.Gallery(label="Generated Images (Preview)", show_label=True, preview=True),