File size: 1,313 Bytes
8e6ae4f
 
73480ac
20a5681
2785c5f
242a937
2785c5f
 
73480ac
 
2785c5f
 
 
 
 
 
 
 
 
 
 
 
 
73480ac
2785c5f
 
 
 
 
 
 
 
 
c1bd806
2785c5f
 
4e4d7b6
2785c5f
 
 
242a937
2785c5f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from PIL import Image, ImageDraw, ImageFont
import io

def text_to_handwriting(text, font_size, font_color):
    try:
        # Define font (Ensure the path to a handwriting font is correct)
        font_path = "Sacramento-Regular.ttf"
        font = ImageFont.truetype(font_path, font_size)

        # Set image size dynamically based on text length
        img_width = max(300, font_size * len(text) // 2)
        img_height = font_size * 3

        # Create image
        img = Image.new("RGB", (img_width, img_height), "white")
        draw = ImageDraw.Draw(img)
        draw.text((10, 10), text, font=font, fill=font_color)

        # Save to BytesIO
        img_bytes = io.BytesIO()
        img.save(img_bytes, format="PNG")
        img_bytes.seek(0)

        return img_bytes
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio UI
iface = gr.Interface(
    fn=text_to_handwriting,
    inputs=[
        gr.Textbox(label="Enter Text", placeholder="Type here..."),
        gr.Slider(minimum=20, maximum=80, value=40, label="Font Size"),
        gr.ColorPicker(label="Font Color")
    ],
    outputs=gr.Image(type="pil"),
    title="Text to Handwriting",
    description="Convert your text into a handwritten-style image."
)

if __name__ == "__main__":
    iface.launch()