Image-To-Text / main.py
ruslanmv's picture
Update main.py
fbf5e14 verified
raw
history blame
3.79 kB
import sys, random, argparse
import numpy as np
import math
from PIL import Image, ImageFont, ImageDraw
import gradio as gr
# 70 levels of gray
gscale1 = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,\\"^`'. "
# 10 levels of gray
gscale2 = '@%#*+=-:. '
def getAverageL(image):
im = np.array(image)
w, h = im.shape
return np.average(im.reshape(w*h))
def covertImageToAscii(input_img, cols, scale, moreLevels):
global gscale1, gscale2
image = Image.fromarray(input_img).convert('L')
W, H = image.size[0], image.size[1]
w = W/cols
h = w/scale
rows = int(H/h)
if cols > W or rows > H:
print("Image too small for specified cols!")
exit(0)
aimg = []
for j in range(rows):
y1 = int(j*h)
y2 = int((j+1)*h)
if j == rows-1:
y2 = H
aimg.append("")
for i in range(cols):
x1 = int(i*w)
x2 = int((i+1)*w)
if i == cols-1:
x2 = W
img = image.crop((x1, y1, x2, y2))
avg = int(getAverageL(img))
if moreLevels:
gsval = gscale1[int((avg*69)/255)]
else:
gsval = gscale2[int((avg*9)/255)]
aimg[j] += gsval
return aimg
def sepia(input_img):
aimg = covertImageToAscii(input_img, 200, 0.43, False)
my_image = Image.new(mode="RGB", size=(2000, 2000), color=(0, 0, 0))
image_editable = ImageDraw.Draw(my_image)
image_editable.text((10, 10), "\n".join(aimg), (237, 230, 211))
return [my_image, "\n".join(aimg)]
iface = gr.Interface(sepia,
gr.inputs.Image(shape=(200, 200)),
["image", "text"],
title = "ASCII Art",
description = "Convert an image to ASCII art based on ascii character density. Copy and paste the text to a notepad to see it correctly")
iface.launch(server_name="0.0.0.0", server_port=7860)
'''
import gradio as gr
import numpy as np
from PIL import Image
def generate_ascii_art(image):
try:
# Convert the numpy array to a PIL Image
img = Image.fromarray(np.uint8(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(server_name="0.0.0.0", server_port=7860)
'''
'''
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)
'''