Kvikontent commited on
Commit
914234b
·
verified ·
1 Parent(s): 747acb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from PIL import Image
3
  import numpy as np
4
 
 
5
  ASCII_CHARS = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
6
 
7
  def scale_image(image, new_width=100):
@@ -15,32 +16,43 @@ def scale_image(image, new_width=100):
15
  def map_pixels_to_ascii_chars(image, range_width=25):
16
  """Maps each pixel to an ascii char based on intensity."""
17
  pixels_in_image = list(image.getdata())
18
- pixels_to_chars = [ASCII_CHARS[pixel_value//range_width] for pixel_value in pixels_in_image]
19
  return "".join(pixels_to_chars)
20
 
21
  def convert_image_to_ascii(image, new_width=100):
22
  image = scale_image(image)
23
- image = image.convert("L")
24
 
25
  pixels_to_chars = map_pixels_to_ascii_chars(image)
26
  len_pixels_to_chars = len(pixels_to_chars)
27
 
 
28
  ascii_image = [pixels_to_chars[index: index + new_width] for index in range(0, len_pixels_to_chars, new_width)]
29
  return "\n".join(ascii_image)
30
 
31
- def image_to_ascii(file):
32
- image = Image.open(file.name)
 
33
  ascii_str = convert_image_to_ascii(image)
34
  return ascii_str
35
 
36
- description = "Image to ASCII Art Converter\nUpload an image and convert it to ASCII art. Click the 'Submit' button after uploading."
 
 
 
 
 
 
 
 
37
  interface = gr.Interface(
38
- fn=image_to_ascii,
39
  inputs=gr.Image(label="Upload Image"),
40
  outputs=gr.Textbox(label="ASCII Art"),
41
- examples=["example1.jpg"],
42
  title="Image to ASCII Art",
43
  description=description,
 
44
  )
45
 
46
  interface.launch()
 
2
  from PIL import Image
3
  import numpy as np
4
 
5
+ # Define ASCII characters according to the intensity scale
6
  ASCII_CHARS = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
7
 
8
  def scale_image(image, new_width=100):
 
16
  def map_pixels_to_ascii_chars(image, range_width=25):
17
  """Maps each pixel to an ascii char based on intensity."""
18
  pixels_in_image = list(image.getdata())
19
+ pixels_to_chars = [ASCII_CHARS[pixel_value // range_width] for pixel_value in pixels_in_image]
20
  return "".join(pixels_to_chars)
21
 
22
  def convert_image_to_ascii(image, new_width=100):
23
  image = scale_image(image)
24
+ image = image.convert("L") # convert image to monochrome
25
 
26
  pixels_to_chars = map_pixels_to_ascii_chars(image)
27
  len_pixels_to_chars = len(pixels_to_chars)
28
 
29
+ # Convert the string of characters into a list of strings
30
  ascii_image = [pixels_to_chars[index: index + new_width] for index in range(0, len_pixels_to_chars, new_width)]
31
  return "\n".join(ascii_image)
32
 
33
+ def image_to_ascii(image):
34
+ # Convert the NumPy array to a PIL Image and then to ASCII
35
+ image = Image.fromarray(image.astype('uint8'), 'RGB')
36
  ascii_str = convert_image_to_ascii(image)
37
  return ascii_str
38
 
39
+ description = """
40
+ Image to ASCII Art Converter\n
41
+ Upload an image and convert it to ASCII art. Simply upload the image and
42
+ click the 'Submit' button to see the ASCII art.
43
+ """
44
+
45
+ examples = ["example.jpg"] # Make sure this example image exists in the same directory as this script
46
+
47
+ # Define the Gradio interface
48
  interface = gr.Interface(
49
+ fn=image_to_ascii,
50
  inputs=gr.Image(label="Upload Image"),
51
  outputs=gr.Textbox(label="ASCII Art"),
52
+ examples=examples,
53
  title="Image to ASCII Art",
54
  description=description,
55
+ allow_flagging="never",
56
  )
57
 
58
  interface.launch()