make a gradio interface for image to ascii art or text 2 ascii art generation. make it realtime. and share to true
Assistant
Sure! Here's a Gradio interface that converts images to ASCII art and text to ASCII art in real-time. The interface includes two tabs: one for image-to-ASCII conversion and another for text-to-ASCII conversion. The share=True parameter enables you to share the app with others.
python
# Install necessary libraries before running:
# pip install gradio pillow pyfiglet
import gradio as gr
from PIL import Image, ImageOps
import numpy as np
import pyfiglet
# ASCII characters used to build the output text
ASCII_CHARS = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
def resize_image(image, new_width=100):
width, height = image.size
aspect_ratio = height / width
new_height = int(new_width * aspect_ratio * 0.55) # Adjust for character aspect ratio
resized_image = image.resize((new_width, new_height))
return resized_image
def image_to_ascii(image):
# Convert image to grayscale
image = ImageOps.grayscale(image)
# Resize the image
image = resize_image(image)
pixels = np.array(image)
# Map each pixel to an ASCII character
ascii_str = ""
for row in pixels:
for pixel in row:
ascii_str += ASCII_CHARS[pixel // 25]
ascii_str += "\n"
return ascii_str
def text_to_ascii_art(text):
# Convert text to ASCII art using pyfiglet
result = pyfiglet.figlet_format(text)
return result
with gr.Blocks() as demo:
gr.Markdown("## Image to ASCII Art Converter")
with gr.Tab("Image to ASCII"):
with gr.Row():
image_input = gr.Image(label="Upload Image")
ascii_output = gr.Textbox(label="ASCII Art Output")
# Update ASCII art in real-time when the image changes
image_input.change(fn=image_to_ascii, inputs=image_input, outputs=ascii_output)
gr.Markdown("## Text to ASCII Art Converter")
with gr.Tab("Text to ASCII"):
with gr.Row():
text_input = gr.Textbox(
label="Enter Text", lines=1, placeholder="Type text here..."
)
ascii_text_output = gr.Textbox(label="ASCII Art Output")
# Update ASCII art in real-time when the text changes
text_input.change(
fn=text_to_ascii_art, inputs=text_input, outputs=ascii_text_output
)
gr.Markdown(
"### Note: The output will update in real time as you change the input."
)
# Launch the app with share=True to enable sharing
demo.launch(share=True)
How to Use the Interface
Image to ASCII Tab:
Upload an image by clicking on the "Upload Image" button.
The ASCII art representation of the image will appear in the "ASCII Art Output" box.
The output updates in real-time whenever you change the input image.
Text to ASCII Tab:
Type your text into the "Enter Text" box.
The ASCII art representation of the text will appear in the "ASCII Art Output" box.
The output updates in real-time as you type.
Explanation of the Code
Imports:
gradio for the interface.
PIL (Python Imaging Library) for image processing.
numpy for handling image arrays.
pyfiglet to create ASCII art from text.
Constants:
ASCII_CHARS: A list of characters used to represent the grayscale values in the image.
Functions:
resize_image(image, new_width=100): Resizes the image while maintaining the aspect ratio.
image_to_ascii(image): Converts the image to grayscale, resizes it, and maps each pixel to an ASCII character.
text_to_ascii_art(text): Converts text to ASCII art using pyfiglet.
Gradio Interface:
Uses gr.Blocks, gr.Tab, and gr.Row for a structured layout.
The change event is used to update outputs in real-time when inputs change.
The share=True parameter in demo.launch() allows you to share the app via a public link.
Running the App
Install Dependencies:
bash
pip install gradio pillow pyfiglet
Run the Script:
bash
python script_name.py
Access the Interface:
Once the script is running, it will provide a local URL (e.g., http://127.0.0.1:7860) and a public URL for sharing.
Open the URL in a web browser to use the interface.
Notes
Real-Time Updates: The interface leverages Gradio's event handling to update the ASCII art output whenever the input changes, providing a real-time experience.
Sharing the App: With share=True, Gradio generates a temporary public link using Gradio's hosting services, which is ideal for demonstrations and sharing with others.
Customization: You can adjust the new_width parameter in the resize_image function to change the width of the ASCII art output.
Feel free to customize and enhance the interface to suit your needs!