Deniel Dimitrov commited on
Commit
de4a0cd
Β·
1 Parent(s): e3b8625

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import numpy as np
4
+
5
+ def convert_to_ascii(image, text_size):
6
+ image = Image.fromarray(image.astype('uint8'))
7
+
8
+ width, height = image.size
9
+ aspect_ratio = height / width
10
+ new_width = text_size
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
+ return ascii_image
29
+
30
+ def image_to_ascii(image, text_size):
31
+ ascii_text = convert_to_ascii(image, text_size)
32
+ return ascii_text
33
+
34
+ iface = gr.Interface(fn=image_to_ascii, inputs=["image", "number"], outputs="text", title="Image to ASCII")
35
+ iface.launch()
36
+