File size: 3,789 Bytes
fbf5e14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66f8fc1
41e193d
6987040
 
 
 
41e193d
 
6987040
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a1b115
fbf5e14
6987040
 
 
5ce139d
 
e8f2900
5ce139d
e8f2900
 
 
 
5ce139d
e8f2900
 
 
 
ffcf10e
e8f2900
 
c7eed2a
 
 
c1f1f88
e8f2900
66f8fc1
6987040
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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)
'''