Create-image / app.py
blind1234's picture
initial commit
4341494 verified
# 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)