Bhavibond commited on
Commit
3316e99
·
verified ·
1 Parent(s): aff59fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -1,40 +1,59 @@
1
  import gradio as gr
2
  import numpy as np
3
- import cv2
4
  from PIL import Image, ImageDraw, ImageFont
 
 
 
 
 
5
 
6
- # List of available handwriting-style fonts
7
  FONTS = {
8
- "Cursive": "https://github.com/google/fonts/raw/main/ofl/dancingscript/DancingScript-Regular.ttf",
9
- "School Notebook": "https://github.com/google/fonts/raw/main/ofl/permanentmarker/PermanentMarker-Regular.ttf",
10
- "Elegant Calligraphy": "https://github.com/google/fonts/raw/main/ofl/sacramento/Sacramento-Regular.ttf"
 
 
 
 
 
 
 
11
  }
12
 
 
 
 
 
 
 
 
13
  # Function to convert text to handwriting
14
  def text_to_handwriting(text, font_choice, font_size, font_color):
15
  if not text.strip():
16
  return None # Return nothing if text is empty
17
 
18
- # Load font
19
- font_path = FONTS[font_choice]
20
- font = ImageFont.truetype(font_path, font_size)
21
 
22
  # Estimate text size
 
23
  text_width, text_height = font.getsize_multiline(text)
24
-
25
- # Create a blank image
26
- img = Image.new("RGB", (text_width + 40, text_height + 40), (255, 255, 255))
 
 
27
  draw = ImageDraw.Draw(img)
28
-
29
- # Draw the text on the image
30
  draw.text((20, 20), text, font=font, fill=font_color)
31
 
32
  return img
33
 
34
  # Gradio UI
35
  with gr.Blocks() as demo:
36
- gr.Markdown("# 🖋️ Text to Handwriting Converter")
37
- gr.Markdown("Convert your typed text into **realistic handwriting styles!**")
38
 
39
  text_input = gr.Textbox(label="Enter Your Text", placeholder="Type something...")
40
  font_selector = gr.Dropdown(list(FONTS.keys()), label="Select Handwriting Style")
@@ -51,4 +70,4 @@ with gr.Blocks() as demo:
51
  outputs=output_image
52
  )
53
 
54
- demo.launch()
 
1
  import gradio as gr
2
  import numpy as np
 
3
  from PIL import Image, ImageDraw, ImageFont
4
+ import os
5
+
6
+ # Save fonts locally for faster access
7
+ FONTS_DIR = "fonts"
8
+ os.makedirs(FONTS_DIR, exist_ok=True)
9
 
 
10
  FONTS = {
11
+ "Cursive": "DancingScript-Regular.ttf",
12
+ "School Notebook": "PermanentMarker-Regular.ttf",
13
+ "Elegant Calligraphy": "Sacramento-Regular.ttf"
14
+ }
15
+
16
+ # Download fonts once and store them locally
17
+ FONT_URLS = {
18
+ "DancingScript-Regular.ttf": "https://github.com/google/fonts/raw/main/ofl/dancingscript/DancingScript-Regular.ttf",
19
+ "PermanentMarker-Regular.ttf": "https://github.com/google/fonts/raw/main/ofl/permanentmarker/PermanentMarker-Regular.ttf",
20
+ "Sacramento-Regular.ttf": "https://github.com/google/fonts/raw/main/ofl/sacramento/Sacramento-Regular.ttf"
21
  }
22
 
23
+ for font_name, url in FONT_URLS.items():
24
+ font_path = os.path.join(FONTS_DIR, font_name)
25
+ if not os.path.exists(font_path): # Download only if not already present
26
+ import requests
27
+ with open(font_path, "wb") as f:
28
+ f.write(requests.get(url).content)
29
+
30
  # Function to convert text to handwriting
31
  def text_to_handwriting(text, font_choice, font_size, font_color):
32
  if not text.strip():
33
  return None # Return nothing if text is empty
34
 
35
+ font_path = os.path.join(FONTS_DIR, FONTS[font_choice])
36
+ font = ImageFont.truetype(font_path, int(font_size))
 
37
 
38
  # Estimate text size
39
+ max_width, max_height = 600, 300 # Limit size to prevent slow rendering
40
  text_width, text_height = font.getsize_multiline(text)
41
+
42
+ # Create image with limited size
43
+ img_width = min(text_width + 40, max_width)
44
+ img_height = min(text_height + 40, max_height)
45
+ img = Image.new("RGB", (img_width, img_height), "white")
46
  draw = ImageDraw.Draw(img)
47
+
48
+ # Draw text
49
  draw.text((20, 20), text, font=font, fill=font_color)
50
 
51
  return img
52
 
53
  # Gradio UI
54
  with gr.Blocks() as demo:
55
+ gr.Markdown("# 🖋️ Text to Handwriting Converter (Fast Version)")
56
+ gr.Markdown("Convert your typed text into **realistic handwriting styles instantly!**")
57
 
58
  text_input = gr.Textbox(label="Enter Your Text", placeholder="Type something...")
59
  font_selector = gr.Dropdown(list(FONTS.keys()), label="Select Handwriting Style")
 
70
  outputs=output_image
71
  )
72
 
73
+ demo.launch()