Spaces:
Running
Running
Deniel Dimitrov
commited on
Commit
Β·
36522c2
1
Parent(s):
ade2e92
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,40 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image
|
3 |
-
import numpy as np
|
4 |
|
5 |
-
def convert_to_ascii(image,
|
6 |
-
image = Image.fromarray(image.astype('uint8'))
|
7 |
-
|
8 |
width, height = image.size
|
9 |
aspect_ratio = height / width
|
10 |
-
new_width =
|
11 |
new_height = int(aspect_ratio * text_size * 0.5)
|
12 |
|
13 |
resized_image = image.resize((new_width, new_height))
|
14 |
grayscale_image = resized_image.convert('L')
|
15 |
|
16 |
-
ascii_chars = '
|
17 |
|
18 |
ascii_image = ''
|
19 |
for y in range(new_height):
|
20 |
for x in range(new_width):
|
21 |
pixel_value = grayscale_image.getpixel((x, y))
|
22 |
-
if pixel_value == 255:
|
23 |
ascii_image += ' '
|
24 |
else:
|
25 |
ascii_image += ascii_chars[int(pixel_value / 255 * (len(ascii_chars) - 1))]
|
26 |
ascii_image += '\n'
|
27 |
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
iface.launch()
|
36 |
-
|
|
|
1 |
+
|
2 |
+
from PIL import Image, ImageTk
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
+
def convert_to_ascii(image,integer):
|
|
|
|
|
6 |
width, height = image.size
|
7 |
aspect_ratio = height / width
|
8 |
+
new_width = integer
|
9 |
new_height = int(aspect_ratio * text_size * 0.5)
|
10 |
|
11 |
resized_image = image.resize((new_width, new_height))
|
12 |
grayscale_image = resized_image.convert('L')
|
13 |
|
14 |
+
ascii_chars = 'β@&%#*β+=-:,.\/|][}{)(Β΄βββββ ' # add more characters if you want
|
15 |
|
16 |
ascii_image = ''
|
17 |
for y in range(new_height):
|
18 |
for x in range(new_width):
|
19 |
pixel_value = grayscale_image.getpixel((x, y))
|
20 |
+
if pixel_value == 255: # make sure to use the GOD DAM transparency
|
21 |
ascii_image += ' '
|
22 |
else:
|
23 |
ascii_image += ascii_chars[int(pixel_value / 255 * (len(ascii_chars) - 1))]
|
24 |
ascii_image += '\n'
|
25 |
|
26 |
+
return ascii_image
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
|
31 |
+
# Create an Interface with a title
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=convert_to_ascii, # Function to run
|
34 |
+
inputs=["image","integer"], # Input component (in this case, an image)
|
35 |
+
outputs="text", # Output component (in this case, text)
|
36 |
+
title="image-to-ascii (by peasoup)" # Title for the interface
|
37 |
+
)
|
38 |
|
39 |
+
# Launch the interface
|
40 |
+
iface.launch()
|
|