image-to-ascii / app.py
Deniel Dimitrov
Update app.py
36522c2
raw
history blame
1.21 kB
from PIL import Image, ImageTk
import gradio as gr
def convert_to_ascii(image,integer):
width, height = image.size
aspect_ratio = height / width
new_width = integer
new_height = int(aspect_ratio * text_size * 0.5)
resized_image = image.resize((new_width, new_height))
grayscale_image = resized_image.convert('L')
ascii_chars = 'β–ˆ@&%#*β–‘+=-:,.\/|][}{)(Β΄β€žβ€Ÿβ€šβ€›β€˜ ' # add more characters if you want
ascii_image = ''
for y in range(new_height):
for x in range(new_width):
pixel_value = grayscale_image.getpixel((x, y))
if pixel_value == 255: # make sure to use the GOD DAM transparency
ascii_image += ' '
else:
ascii_image += ascii_chars[int(pixel_value / 255 * (len(ascii_chars) - 1))]
ascii_image += '\n'
return ascii_image
# Create an Interface with a title
iface = gr.Interface(
fn=convert_to_ascii, # Function to run
inputs=["image","integer"], # Input component (in this case, an image)
outputs="text", # Output component (in this case, text)
title="image-to-ascii (by peasoup)" # Title for the interface
)
# Launch the interface
iface.launch()