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()