File size: 1,079 Bytes
86cfa7a
2ebe0d6
 
de4a0cd
 
2ebe0d6
 
de4a0cd
 
f977167
de4a0cd
 
 
 
 
ade2e92
de4a0cd
 
 
 
 
2ebe0d6
de4a0cd
 
 
 
 
 
 
aefbe4c
2ebe0d6
 
86cfa7a
2ebe0d6
de4a0cd
565f72e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import gradio as gr
from PIL import Image
import numpy as np

def convert_to_ascii(image, text_size):
    image = Image.fromarray(image.astype('uint8'))
    
    width, height = image.size
    aspect_ratio = height / width
    new_width = int(text_size)
    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 = '█@&%#*░+=-:,.\/´'

    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:
                ascii_image += ' '
            else:
                ascii_image += ascii_chars[int(pixel_value / 255 * (len(ascii_chars) - 1))]
        ascii_image += '\n'

    return ascii_image

def image_to_ascii(image, text_size):
    ascii_text = convert_to_ascii(image, text_size)
    return ascii_text

iface = gr.Interface(fn=image_to_ascii, inputs=["image", "number"], outputs="text", title="Image to ASCII")
iface.launch()