Sankie005 commited on
Commit
bd90548
·
verified ·
1 Parent(s): 7d83662

Create app.py

Browse files

uploaded app.py

Files changed (1) hide show
  1. app.py +146 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import ascii_magic
3
+ from PIL import Image
4
+
5
+ def sanitize_ascii_string(ascii_str):
6
+ replacements = {
7
+ '<': '〈',
8
+ '>': '〉',
9
+ '&': '&',
10
+ '"': '"',
11
+ "'": ''',
12
+ }
13
+ for key, value in replacements.items():
14
+ ascii_str = ascii_str.replace(key, value)
15
+ return ascii_str
16
+
17
+ def normalize_lines(ascii_str):
18
+ lines = ascii_str.split('\n')
19
+ max_length = max(len(line) for line in lines)
20
+ normalized_lines = [line.lstrip().ljust(max_length) for line in lines] # Remove leading whitespace and pad to max length
21
+ return '\n'.join(normalized_lines)
22
+
23
+ def convert_to_ascii(image, columns=100, font_size=10, width=256, height=256):
24
+ if not isinstance(image, Image.Image):
25
+ image = Image.open(image)
26
+
27
+ # Convert image to grayscale
28
+ image = image.convert('L')
29
+
30
+ # Resize the image to the specified width and height
31
+ image = image.resize((width, height))
32
+
33
+ # Generate ASCII art
34
+ ascii_art = ascii_magic.from_pillow_image(image)
35
+ ascii_html = ascii_art.to_html(columns=int(columns), monochrome=True)
36
+
37
+ # Add CSS to maintain aspect ratio and improve display
38
+ html_output = f"""
39
+ <div style="white-space: pre; font-family: monospace; line-height: 1; font-size: {font_size}px; background-color: black; color: white;">{ascii_html}</div>
40
+ """
41
+
42
+ # Remove leading spaces from the first line
43
+ lines = ascii_html.split("\n")
44
+ if lines:
45
+ lines[0] = lines[0].lstrip()
46
+ ascii_html = "\n".join(lines)
47
+
48
+ # Save the HTML to a file
49
+ html_file_path = "ascii_art.html"
50
+ with open(html_file_path, "w", encoding="utf-8") as file:
51
+ file.write(html_output)
52
+
53
+ return html_output, html_file_path
54
+
55
+ with gr.Blocks(title="ASCII Art Converter") as demo:
56
+ gr.Markdown("# Image to ASCII Art Converter By Sankritya Anand Rai")
57
+ gr.Markdown("Convert images to detailed ASCII art. Adjust the resolution, font size, and image dimensions to control the output.")
58
+
59
+ with gr.Row():
60
+ with gr.Column(scale=1):
61
+ # Input controls
62
+ input_image = gr.Image(
63
+ label="Input Image",
64
+ type="pil",
65
+ height=300
66
+ )
67
+
68
+ with gr.Row():
69
+ resolution_slider = gr.Slider(
70
+ minimum=32,
71
+ maximum=200,
72
+ value=100,
73
+ step=1,
74
+ label="Resolution (columns)",
75
+ info="Higher values = more detail"
76
+ )
77
+ font_slider = gr.Slider(
78
+ minimum=6,
79
+ maximum=20,
80
+ value=10,
81
+ step=1,
82
+ label="Font Size (px)",
83
+ info="Adjust text size"
84
+ )
85
+ width_slider = gr.Slider(
86
+ minimum=64,
87
+ maximum=512,
88
+ value=256,
89
+ step=1,
90
+ label="Image Width (px)",
91
+ info="Adjust the width of the image"
92
+ )
93
+ height_slider = gr.Slider(
94
+ minimum=64,
95
+ maximum=512,
96
+ value=256,
97
+ step=1,
98
+ label="Image Height (px)",
99
+ info="Adjust the height of the image"
100
+ )
101
+
102
+ convert_btn = gr.Button("Convert to ASCII", variant="primary")
103
+
104
+ with gr.Column(scale=1):
105
+ # Output display
106
+ output_html = gr.HTML(label="ASCII Art Output")
107
+ download_html = gr.File(label="Download HTML File")
108
+
109
+ # Set up event handler
110
+ convert_btn.click(
111
+ fn=convert_to_ascii,
112
+ inputs=[input_image, resolution_slider, font_slider, width_slider, height_slider],
113
+ outputs=[output_html, download_html]
114
+ )
115
+
116
+ # Also enable live updates when sliders change
117
+ resolution_slider.change(
118
+ fn=convert_to_ascii,
119
+ inputs=[input_image, resolution_slider, font_slider, width_slider, height_slider],
120
+ outputs=[output_html, download_html]
121
+ )
122
+ font_slider.change(
123
+ fn=convert_to_ascii,
124
+ inputs=[input_image, resolution_slider, font_slider, width_slider, height_slider],
125
+ outputs=[output_html, download_html]
126
+ )
127
+ width_slider.change(
128
+ fn=convert_to_ascii,
129
+ inputs=[input_image, resolution_slider, font_slider, width_slider, height_slider],
130
+ outputs=[output_html, download_html]
131
+ )
132
+ height_slider.change(
133
+ fn=convert_to_ascii,
134
+ inputs=[input_image, resolution_slider, font_slider, width_slider, height_slider],
135
+ outputs=[output_html, download_html]
136
+ )
137
+
138
+ # Enable generation when image is uploaded
139
+ input_image.change(
140
+ fn=convert_to_ascii,
141
+ inputs=[input_image, resolution_slider, font_slider, width_slider, height_slider],
142
+ outputs=[output_html, download_html]
143
+ )
144
+
145
+ if __name__ == "__main__":
146
+ demo.launch(share=True)