Image-To-Text / main.py
ruslanmv's picture
Update main.py
6987040 verified
raw
history blame
1.71 kB
import gradio as gr
from PIL import Image
def generate_ascii_art(image):
try:
# Open the image using Pillow
img = Image.open(image)
# Resize the image to a smaller size for faster processing
img = img.resize((80, 60))
# Convert the image to grayscale
img = img.convert("L")
# Define ASCII characters to represent different intensity levels
ascii_chars = "@%#*+=-:. "
# Convert each pixel to ASCII character based on intensity
ascii_image = ""
for pixel_value in img.getdata():
ascii_image += ascii_chars[pixel_value // 25]
# Reshape the ASCII string to match the resized image dimensions
ascii_image = "\n".join([ascii_image[i:i + img.width] for i in range(0, len(ascii_image), img.width)])
return ascii_image
except Exception as e:
return f"Error: {e}"
iface = gr.Interface(
fn=generate_ascii_art,
inputs="image",
outputs="text",
title="ASCII Art Generator",
description="Upload an image, and this app will turn it into ASCII art!",
live=True
)
iface.launch()
'''
import gradio as gr
import subprocess
def run_command(command):
try:
result = subprocess.check_output(command, shell=True, text=True)
return result
except subprocess.CalledProcessError as e:
return f"Error: {e}"
iface = gr.Interface(
fn=run_command,
inputs="text",
outputs="text",
#live=True,
title="Command Output Viewer",
description="Enter a command and view its output.",
examples=[
["ls"],
["pwd"],
["echo 'Hello, Gradio!'"]]
)
iface.launch(server_name="0.0.0.0", server_port=7860)
'''