Deniel Dimitrov commited on
Commit
4d8f7d0
Β·
1 Parent(s): 7d8028a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -1,4 +1,6 @@
1
- from PIL import Image
 
 
2
  import gradio as gr
3
 
4
  def convert_to_ascii(image, text_size):
@@ -10,13 +12,13 @@ def convert_to_ascii(image, text_size):
10
  resized_image = image.resize((new_width, new_height))
11
  grayscale_image = resized_image.convert('L')
12
 
13
- ascii_chars = 'β–ˆ@&%#*β–‘+=-:,.\/|][}{)(Β΄β€›β€˜ ' # add more characters if you want
14
 
15
  ascii_image = ''
16
  for y in range(new_height):
17
  for x in range(new_width):
18
  pixel_value = grayscale_image.getpixel((x, y))
19
- if pixel_value == 255:
20
  ascii_image += ' '
21
  else:
22
  ascii_image += ascii_chars[int(pixel_value / 255 * (len(ascii_chars) - 1))]
@@ -24,12 +26,21 @@ def convert_to_ascii(image, text_size):
24
 
25
  return ascii_image
26
 
27
- def image_to_ascii(img):
28
- image = Image.open(img.name)
29
- text_size = int(img_size)
30
- ascii_art = convert_to_ascii(image, text_size)
31
- return ascii_art
32
 
33
- iface = gr.Interface(fn=image_to_ascii, inputs=["image", "number"], outputs="text", title="Image to ASCII")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  iface.launch()
35
-
 
1
+ import tkinter as tk
2
+ from tkinter import filedialog
3
+ from PIL import Image, ImageTk
4
  import gradio as gr
5
 
6
  def convert_to_ascii(image, text_size):
 
12
  resized_image = image.resize((new_width, new_height))
13
  grayscale_image = resized_image.convert('L')
14
 
15
+ ascii_chars = 'β–ˆ@&%#*β–‘+=-:,.\/|][}{)(Β΄β€žβ€Ÿβ€šβ€›β€˜ ' # add more characters if you want
16
 
17
  ascii_image = ''
18
  for y in range(new_height):
19
  for x in range(new_width):
20
  pixel_value = grayscale_image.getpixel((x, y))
21
+ if pixel_value == 255: # make sure to use the GOD DAM transparency
22
  ascii_image += ' '
23
  else:
24
  ascii_image += ascii_chars[int(pixel_value / 255 * (len(ascii_chars) - 1))]
 
26
 
27
  return ascii_image
28
 
 
 
 
 
 
29
 
30
+ def image_to_ascii(file, text_size):
31
+ image = Image.open(file.name)
32
+ return convert_to_ascii(image, int(text_size))
33
+
34
+
35
+ iface = gr.Interface(
36
+ fn=image_to_ascii,
37
+ inputs=[
38
+ gr.inputs.File(label="Upload Image"),
39
+ gr.inputs.Number(label="Text Size", default=100)
40
+ ],
41
+ outputs=gr.outputs.Textbox(label="ASCII Art"),
42
+ title="Image to ASCII Art",
43
+ description="Convert an image to ASCII art."
44
+ )
45
+
46
  iface.launch()