File size: 1,725 Bytes
afecd71
86cfa7a
afecd71
 
 
 
 
 
 
 
2cae52b
de4a0cd
578cc47
2cae52b
 
 
 
d883fba
de4a0cd
 
578cc47
de4a0cd
 
0a7504b
de4a0cd
 
 
 
 
2cae52b
0a7504b
de4a0cd
 
 
 
2afd058
36522c2
 
 
de4a0cd
2cae52b
36522c2
 
 
d883fba
fb3b04f
a75b059
 
36522c2
86cfa7a
36522c2
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from PIL import Image
import gradio as gr
import numpy as np

def convert_to_ascii(image, text_size):
    if isinstance(image, str):
        gimage = Image.open(image)
    elif isinstance(image, np.ndarray):
        gimage = Image.fromarray(image)
    else:
        raise ValueError("Unsupported input type. Please provide a file path or a NumPy array.")

    width, height = gimage.size
    
    # Calculate a new aspect ratio based on text_size and original image dimensions
    aspect_ratio = height / (width * 2)  # Adjusted aspect ratio
    
    new_width = int(text_size)
    new_height = int(aspect_ratio * text_size * 0.5)

    resized_image = gimage.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:
                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","number"],  # Input component (in this case, an image)
    outputs=gr.Textbox(show_copy_button=True),  # Output component (in this case, text)
    title="image-to-ascii (by peasoup)",
    description="so i made this for mobile people and mac users (you're welcome!!), look at this omg: https://github.com/itspeasoup/image-to-ascii"
)

# Launch the interface
iface.launch()