File size: 2,269 Bytes
4341494
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# This is a Gradio app to convert text into images with adjustable colors, fonts, and alignment.
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np

# Function to convert text to an image
def text_to_image(text, font_size, font_color, bg_color, text_align, width, height):
    # Create a figure and axis with specified width and height
    fig, ax = plt.subplots(figsize=(width, height))
    
    # Set the background color
    ax.set_facecolor(bg_color)
    
    # Display the text with specified alignment
    ax.text(0.5, 0.5, text, fontsize=font_size, color=font_color, ha=text_align, va='center')
    
    # Remove axes
    ax.axis('off')
    
    # Convert the plot to an image
    fig.canvas.draw()
    img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
    
    # Close the plot
    plt.close(fig)
    
    return img

# Create a Gradio interface
with gr.Blocks() as demo:
    # Input components
    text_input = gr.Textbox(label="Text", placeholder="Enter your text here")
    font_size_slider = gr.Slider(10, 100, value=20, label="Font Size")
    font_color_picker = gr.ColorPicker(label="Font Color", value="#000000")
    bg_color_picker = gr.ColorPicker(label="Background Color", value="#FFFFFF")
    text_align_dropdown = gr.Dropdown(choices=["left", "center", "right"], value="center", label="Text Alignment")
    width_slider = gr.Slider(1, 10, value=5, label="Width")
    height_slider = gr.Slider(1, 10, value=5, label="Height")
    
    # Output component
    image_output = gr.Image(label="Generated Image")
    
    # Define the event listener
    create_button = gr.Button("Create")
    create_button.click(
        fn=text_to_image,
        inputs=[text_input, font_size_slider, font_color_picker, bg_color_picker, text_align_dropdown, width_slider, height_slider],
        outputs=image_output
    )
    
    # Initial call to generate the image
    create_button.click(
        fn=text_to_image,
        inputs=[text_input, font_size_slider, font_color_picker, bg_color_picker, text_align_dropdown, width_slider, height_slider],
        outputs=image_output
    )

# Launch the interface
if __name__ == "__main__":
    demo.launch(show_error=True)