Spaces:
Sleeping
Sleeping
alisher123
commited on
Commit
•
f92535b
1
Parent(s):
d9be374
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,9 @@ from PIL import Image
|
|
3 |
import tempfile
|
4 |
import os
|
5 |
|
6 |
-
|
7 |
|
8 |
-
def resize_image(image, new_width
|
9 |
width, height = image.size
|
10 |
aspect_ratio = height / width
|
11 |
new_height = int(aspect_ratio * new_width * 0.55)
|
@@ -14,45 +14,86 @@ def resize_image(image, new_width=100):
|
|
14 |
def to_grayscale(image):
|
15 |
return image.convert('L')
|
16 |
|
17 |
-
def pixel_to_ascii(image):
|
18 |
pixels = list(image.getdata())
|
19 |
-
return ''.join([
|
20 |
|
21 |
-
def image_to_ascii(image, new_width
|
22 |
image = resize_image(image, new_width)
|
23 |
image = to_grayscale(image)
|
24 |
-
ascii_str = pixel_to_ascii(image)
|
25 |
img_width = image.width
|
26 |
return '\n'.join([ascii_str[i:i+img_width] for i in range(0, len(ascii_str), img_width)])
|
27 |
|
28 |
-
def generate_ascii_art(input_image):
|
29 |
if input_image is None:
|
30 |
return "Please upload an image.", None
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
# Create a temporary file to store the ASCII art
|
35 |
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as temp_file:
|
36 |
temp_file.write(ascii_art)
|
37 |
|
38 |
return ascii_art, temp_file.name
|
39 |
|
40 |
-
def process_and_download(input_image):
|
41 |
-
ascii_art, temp_file_path = generate_ascii_art(input_image)
|
42 |
return ascii_art, temp_file_path
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
if __name__ == "__main__":
|
58 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
3 |
import tempfile
|
4 |
import os
|
5 |
|
6 |
+
DEFAULT_ASCII_CHARS = '@#S%?*+;:,. '
|
7 |
|
8 |
+
def resize_image(image, new_width):
|
9 |
width, height = image.size
|
10 |
aspect_ratio = height / width
|
11 |
new_height = int(aspect_ratio * new_width * 0.55)
|
|
|
14 |
def to_grayscale(image):
|
15 |
return image.convert('L')
|
16 |
|
17 |
+
def pixel_to_ascii(image, ascii_chars):
|
18 |
pixels = list(image.getdata())
|
19 |
+
return ''.join([ascii_chars[pixel * len(ascii_chars) // 256] for pixel in pixels])
|
20 |
|
21 |
+
def image_to_ascii(image, ascii_chars, new_width):
|
22 |
image = resize_image(image, new_width)
|
23 |
image = to_grayscale(image)
|
24 |
+
ascii_str = pixel_to_ascii(image, ascii_chars)
|
25 |
img_width = image.width
|
26 |
return '\n'.join([ascii_str[i:i+img_width] for i in range(0, len(ascii_str), img_width)])
|
27 |
|
28 |
+
def generate_ascii_art(input_image, ascii_chars, density):
|
29 |
if input_image is None:
|
30 |
return "Please upload an image.", None
|
31 |
|
32 |
+
# Ensure the ASCII characters are in descending order of darkness
|
33 |
+
ascii_chars = ''.join(sorted(set(ascii_chars), key=lambda c: -ord(c)))
|
34 |
+
|
35 |
+
# Calculate new width based on density
|
36 |
+
new_width = int(density)
|
37 |
+
|
38 |
+
ascii_art = image_to_ascii(input_image, ascii_chars, new_width)
|
39 |
|
|
|
40 |
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as temp_file:
|
41 |
temp_file.write(ascii_art)
|
42 |
|
43 |
return ascii_art, temp_file.name
|
44 |
|
45 |
+
def process_and_download(input_image, ascii_chars, density):
|
46 |
+
ascii_art, temp_file_path = generate_ascii_art(input_image, ascii_chars, density)
|
47 |
return ascii_art, temp_file_path
|
48 |
|
49 |
+
custom_css = """
|
50 |
+
#ascii-output {
|
51 |
+
font-family: monospace;
|
52 |
+
white-space: pre;
|
53 |
+
overflow-x: auto;
|
54 |
+
font-size: 5px;
|
55 |
+
line-height: 1;
|
56 |
+
}
|
57 |
+
"""
|
58 |
+
|
59 |
+
with gr.Blocks(css=custom_css) as iface:
|
60 |
+
gr.Markdown("# Image to ASCII Art Converter")
|
61 |
+
gr.Markdown("Upload an image, choose ASCII characters, set density, convert to ASCII art, and download the result!")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
input_image = gr.Image(type="pil", label="Upload Image")
|
65 |
+
|
66 |
+
with gr.Row():
|
67 |
+
ascii_chars_input = gr.Textbox(
|
68 |
+
label="ASCII Characters",
|
69 |
+
value=DEFAULT_ASCII_CHARS,
|
70 |
+
info="Enter characters from darkest to lightest. The script will remove duplicates and sort them."
|
71 |
+
)
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
density_slider = gr.Slider(
|
75 |
+
minimum=50,
|
76 |
+
maximum=600,
|
77 |
+
value=200,
|
78 |
+
step=10,
|
79 |
+
label="ASCII Art Density",
|
80 |
+
info="Higher values create denser, more detailed ASCII art."
|
81 |
+
)
|
82 |
+
|
83 |
+
with gr.Row():
|
84 |
+
convert_button = gr.Button("Convert to ASCII")
|
85 |
+
|
86 |
+
with gr.Row():
|
87 |
+
ascii_output = gr.Textbox(label="ASCII Art Output", elem_id="ascii-output", lines=50)
|
88 |
+
|
89 |
+
with gr.Row():
|
90 |
+
download_button = gr.File(label="Download ASCII Art")
|
91 |
+
|
92 |
+
convert_button.click(
|
93 |
+
process_and_download,
|
94 |
+
inputs=[input_image, ascii_chars_input, density_slider],
|
95 |
+
outputs=[ascii_output, download_button]
|
96 |
+
)
|
97 |
|
98 |
if __name__ == "__main__":
|
99 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|