Bhavibond commited on
Commit
20a5681
·
verified ·
1 Parent(s): 93a8c5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -31
app.py CHANGED
@@ -1,59 +1,49 @@
1
  import gradio as gr
2
- import numpy as np
3
  from PIL import Image, ImageDraw, ImageFont
4
- import os
5
-
6
- # Load fonts from the local "fonts" directory
7
- FONT_PATHS = {
8
- #"Cursive": "DancingScript-Regular.ttf",
9
- #"School Notebook": "PermanentMarker-Regular.ttf",
10
- "Elegant Calligraphy": "Sacramento-Regular.ttf"
11
- }
12
-
13
- # Ensure fonts exist
14
- for font_name, font_path in FONT_PATHS.items():
15
- if not os.path.exists(font_path):
16
- raise FileNotFoundError(f"Font file missing: {font_path}. Please upload fonts manually.")
17
-
18
- # Function to convert text to handwriting
19
- def text_to_handwriting(text, font_choice, font_size, font_color):
20
  if not text.strip():
21
  return None # Return nothing if text is empty
22
 
23
- # Load the selected font
24
- font = ImageFont.truetype(FONT_PATHS[font_choice], font_size)
25
 
26
  # Estimate text size
27
  text_width, text_height = font.getsize_multiline(text)
28
 
29
- # Create a blank white image with padding
30
- img = Image.new("RGB", (text_width + 40, text_height + 40), (255, 255, 255))
31
- draw = ImageDraw.Draw(img)
 
32
 
33
- # Draw the text on the image
 
34
  draw.text((20, 20), text, font=font, fill=font_color)
35
 
36
  return img
37
 
38
  # Gradio UI
39
  with gr.Blocks() as demo:
40
- gr.Markdown("# 🖋️ Text to Handwriting Converter")
41
- gr.Markdown("Convert your typed text into **realistic handwriting styles!**")
42
 
43
- text_input = gr.Textbox(label="Enter Your Text", placeholder="Type something...")
44
- font_selector = gr.Dropdown(list(FONT_PATHS.keys()), label="Select Handwriting Style")
45
  font_size = gr.Slider(20, 80, value=40, label="Font Size")
46
  font_color = gr.ColorPicker(label="Font Color", value="#000000")
47
 
48
  output_image = gr.Image(label="Generated Handwriting", type="pil")
49
 
50
- submit_button = gr.Button("Convert to Handwriting")
51
 
52
  submit_button.click(
53
  text_to_handwriting,
54
- inputs=[text_input, font_selector, font_size, font_color],
55
  outputs=output_image
56
  )
57
 
58
- # Launch app with optimized settings
59
- demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
 
2
  from PIL import Image, ImageDraw, ImageFont
3
+
4
+ # Default system font (safer for Hugging Face)
5
+ DEFAULT_FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
6
+
7
+ # Function to generate handwriting
8
+ def text_to_handwriting(text, font_size, font_color):
 
 
 
 
 
 
 
 
 
 
9
  if not text.strip():
10
  return None # Return nothing if text is empty
11
 
12
+ # Load font
13
+ font = ImageFont.truetype(DEFAULT_FONT, font_size)
14
 
15
  # Estimate text size
16
  text_width, text_height = font.getsize_multiline(text)
17
 
18
+ # Create a white canvas (limit max size for performance)
19
+ max_width = min(text_width + 40, 800)
20
+ max_height = min(text_height + 40, 600)
21
+ img = Image.new("RGB", (max_width, max_height), "white")
22
 
23
+ # Draw text
24
+ draw = ImageDraw.Draw(img)
25
  draw.text((20, 20), text, font=font, fill=font_color)
26
 
27
  return img
28
 
29
  # Gradio UI
30
  with gr.Blocks() as demo:
31
+ gr.Markdown("# ️ Text to Handwriting Converter")
32
+ gr.Markdown("Type text and convert it to **handwritten style**!")
33
 
34
+ text_input = gr.Textbox(label="Enter Text", placeholder="Write something...")
 
35
  font_size = gr.Slider(20, 80, value=40, label="Font Size")
36
  font_color = gr.ColorPicker(label="Font Color", value="#000000")
37
 
38
  output_image = gr.Image(label="Generated Handwriting", type="pil")
39
 
40
+ submit_button = gr.Button("Generate")
41
 
42
  submit_button.click(
43
  text_to_handwriting,
44
+ inputs=[text_input, font_size, font_color],
45
  outputs=output_image
46
  )
47
 
48
+ # Optimized Gradio launch
49
+ demo.launch(share=True, server_name="0.0.0.0", server_port=7860, debug=True)