Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
| 41 |
+
font_size = gr.Slider(20, 80, value=40, label="Font Size")
|
| 42 |
+
font_color = gr.ColorPicker(label="Font Color", value="#000000")
|
| 43 |
+
|
| 44 |
+
output_image = gr.Image(label="Generated Handwriting", type="pil")
|
| 45 |
+
|
| 46 |
+
submit_button = gr.Button("Convert to Handwriting")
|
| 47 |
+
|
| 48 |
+
submit_button.click(
|
| 49 |
+
text_to_handwriting,
|
| 50 |
+
inputs=[text_input, font_selector, font_size, font_color],
|
| 51 |
+
outputs=output_image
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
demo.launch()
|