ArrcttacsrjksX commited on
Commit
dc08df9
·
verified ·
1 Parent(s): 5a1461e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -9,6 +9,7 @@ import math
9
  import tempfile
10
  from pathlib import Path
11
 
 
12
  COMMON_FONTS = [
13
  "Times New Roman",
14
  "Arial",
@@ -149,10 +150,10 @@ def render_math_image(text, font_size, width, height, bg_color, text_color):
149
  return img
150
 
151
  def text_to_image(input_text, font_size, width, height, bg_color, text_color,
152
- mode, font_name, align, image_format, preview_mode):
153
  if mode == "Plain Text":
154
  img = render_plain_text_image(input_text, font_size, width, height,
155
- bg_color, text_color, font_name, align)
156
  elif mode == "LaTeX Math":
157
  img = render_math_image(input_text, font_size, width, height, bg_color, text_color)
158
  else:
@@ -164,18 +165,18 @@ def text_to_image(input_text, font_size, width, height, bg_color, text_color,
164
  return save_image_to_file(img, image_format)
165
 
166
  def handle_file_upload(file, font_size, width, height, bg_color, text_color,
167
- mode, font_name, align, image_format, preview_mode):
168
  if file is not None:
169
  file_path = file[0]
170
  with open(file_path, "r", encoding="utf-8") as f:
171
  text = f.read()
172
  return text_to_image(text, font_size, width, height, bg_color, text_color,
173
- mode, font_name, align, image_format, preview_mode)
174
  return "No file uploaded!"
175
 
 
176
  font_list, FONT_PATHS = get_system_fonts()
177
- default_font = next((f for f in font_list if "times" in f.lower()
178
- or "arial" in f.lower()), font_list[0])
179
 
180
  with gr.Blocks() as demo:
181
  gr.Markdown("# 🖼️ Text to Image Converter")
@@ -201,30 +202,27 @@ with gr.Blocks() as demo:
201
  mode = gr.Radio(["Plain Text", "LaTeX Math"], label="Rendering Mode", value="Plain Text")
202
  image_format = gr.Radio(["PNG", "JPEG"], label="Image Format", value="PNG")
203
  preview_mode = gr.Checkbox(label="Preview Mode", value=True,
204
- info="Uncheck to get download link instead of preview")
205
 
206
- output = gr.Variable()
 
207
  preview_image = gr.Image(label="Preview", visible=True)
208
  download_link = gr.File(label="Download Image", visible=False)
209
 
 
210
  def update_output(result, preview_mode):
211
  if preview_mode:
212
- return {
213
- preview_image: result,
214
- download_link: gr.update(visible=False),
215
- preview_image: gr.update(visible=True)
216
- }
217
  else:
218
- return {
219
- download_link: result,
220
- preview_image: gr.update(visible=False),
221
- download_link: gr.update(visible=True)
222
- }
223
 
224
  with gr.Row():
225
  convert_button = gr.Button("Convert Text to Image")
226
  file_convert_button = gr.Button("Convert File to Image")
227
 
 
228
  convert_button.click(
229
  text_to_image,
230
  inputs=[
@@ -238,6 +236,7 @@ with gr.Blocks() as demo:
238
  outputs=[preview_image, download_link]
239
  )
240
 
 
241
  file_convert_button.click(
242
  handle_file_upload,
243
  inputs=[
@@ -257,4 +256,4 @@ with gr.Blocks() as demo:
257
  outputs=[preview_image, download_link]
258
  )
259
 
260
- demo.launch()
 
9
  import tempfile
10
  from pathlib import Path
11
 
12
+ # A list of common fonts to prioritize
13
  COMMON_FONTS = [
14
  "Times New Roman",
15
  "Arial",
 
150
  return img
151
 
152
  def text_to_image(input_text, font_size, width, height, bg_color, text_color,
153
+ mode, font_name, align, image_format, preview_mode):
154
  if mode == "Plain Text":
155
  img = render_plain_text_image(input_text, font_size, width, height,
156
+ bg_color, text_color, font_name, align)
157
  elif mode == "LaTeX Math":
158
  img = render_math_image(input_text, font_size, width, height, bg_color, text_color)
159
  else:
 
165
  return save_image_to_file(img, image_format)
166
 
167
  def handle_file_upload(file, font_size, width, height, bg_color, text_color,
168
+ mode, font_name, align, image_format, preview_mode):
169
  if file is not None:
170
  file_path = file[0]
171
  with open(file_path, "r", encoding="utf-8") as f:
172
  text = f.read()
173
  return text_to_image(text, font_size, width, height, bg_color, text_color,
174
+ mode, font_name, align, image_format, preview_mode)
175
  return "No file uploaded!"
176
 
177
+ # Retrieve the system fonts and choose a default font.
178
  font_list, FONT_PATHS = get_system_fonts()
179
+ default_font = next((f for f in font_list if "times" in f.lower() or "arial" in f.lower()), font_list[0])
 
180
 
181
  with gr.Blocks() as demo:
182
  gr.Markdown("# 🖼️ Text to Image Converter")
 
202
  mode = gr.Radio(["Plain Text", "LaTeX Math"], label="Rendering Mode", value="Plain Text")
203
  image_format = gr.Radio(["PNG", "JPEG"], label="Image Format", value="PNG")
204
  preview_mode = gr.Checkbox(label="Preview Mode", value=True,
205
+ info="Uncheck to get download link instead of preview")
206
 
207
+ # Replace gr.Variable with gr.State for maintaining state
208
+ output = gr.State()
209
  preview_image = gr.Image(label="Preview", visible=True)
210
  download_link = gr.File(label="Download Image", visible=False)
211
 
212
+ # The update function now returns a tuple of two outputs.
213
  def update_output(result, preview_mode):
214
  if preview_mode:
215
+ # When in preview mode, show the image preview and hide the download link.
216
+ return result, gr.update(visible=False)
 
 
 
217
  else:
218
+ # Otherwise, hide the preview and show the download link.
219
+ return gr.update(visible=False), result
 
 
 
220
 
221
  with gr.Row():
222
  convert_button = gr.Button("Convert Text to Image")
223
  file_convert_button = gr.Button("Convert File to Image")
224
 
225
+ # When clicking the convert button, first run text_to_image and then update the outputs.
226
  convert_button.click(
227
  text_to_image,
228
  inputs=[
 
236
  outputs=[preview_image, download_link]
237
  )
238
 
239
+ # For file upload conversion:
240
  file_convert_button.click(
241
  handle_file_upload,
242
  inputs=[
 
256
  outputs=[preview_image, download_link]
257
  )
258
 
259
+ demo.launch()