Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -88,7 +88,6 @@ with gr.Blocks() as demo:
|
|
| 88 |
max_lines=50,
|
| 89 |
show_copy_button=True
|
| 90 |
)
|
| 91 |
-
output_image = gr.Image(label="ASCII Art Image Preview", type="pil")
|
| 92 |
|
| 93 |
# Update character set based on dropdown selection
|
| 94 |
def update_char_set(character_set, custom_chars):
|
|
@@ -113,29 +112,13 @@ with gr.Blocks() as demo:
|
|
| 113 |
chars = char_sets.get(character_set, "@%#*+=-:. ")
|
| 114 |
|
| 115 |
if image is None:
|
| 116 |
-
return "Please upload an image first!"
|
| 117 |
|
| 118 |
try:
|
| 119 |
ascii_result = image_to_ascii(image, width, chars)
|
| 120 |
-
|
| 121 |
-
lines = ascii_result.split("\n")
|
| 122 |
-
h = len(lines)
|
| 123 |
-
w = max(len(line) for line in lines) if h > 0 else 0
|
| 124 |
-
if w == 0 or h == 0:
|
| 125 |
-
preview = None
|
| 126 |
-
else:
|
| 127 |
-
# Map characters to grayscale values
|
| 128 |
-
unique_chars = list(dict.fromkeys(chars))
|
| 129 |
-
num_chars = len(unique_chars)
|
| 130 |
-
char_to_val = {c: int((i / max(num_chars - 1, 1)) * 255) for i, c in enumerate(unique_chars)}
|
| 131 |
-
img_array = np.zeros((h, w), dtype=np.uint8)
|
| 132 |
-
for y, line in enumerate(lines):
|
| 133 |
-
for x, ch in enumerate(line):
|
| 134 |
-
img_array[y, x] = char_to_val.get(ch, 0)
|
| 135 |
-
preview = np.stack([img_array]*3, axis=-1)
|
| 136 |
-
return f"\n{ascii_result}\n", preview
|
| 137 |
except Exception as e:
|
| 138 |
-
return f"Error processing image: {str(e)}"
|
| 139 |
|
| 140 |
# Connect the components
|
| 141 |
char_set_dropdown.change(
|
|
@@ -147,7 +130,7 @@ with gr.Blocks() as demo:
|
|
| 147 |
submit_btn.click(
|
| 148 |
process_with_char_set,
|
| 149 |
inputs=[image_input, width_slider, char_set_dropdown, custom_chars],
|
| 150 |
-
outputs=[output_text
|
| 151 |
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
|
|
|
| 88 |
max_lines=50,
|
| 89 |
show_copy_button=True
|
| 90 |
)
|
|
|
|
| 91 |
|
| 92 |
# Update character set based on dropdown selection
|
| 93 |
def update_char_set(character_set, custom_chars):
|
|
|
|
| 112 |
chars = char_sets.get(character_set, "@%#*+=-:. ")
|
| 113 |
|
| 114 |
if image is None:
|
| 115 |
+
return "Please upload an image first!"
|
| 116 |
|
| 117 |
try:
|
| 118 |
ascii_result = image_to_ascii(image, width, chars)
|
| 119 |
+
return f"\n{ascii_result}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
+
return f"Error processing image: {str(e)}"
|
| 122 |
|
| 123 |
# Connect the components
|
| 124 |
char_set_dropdown.change(
|
|
|
|
| 130 |
submit_btn.click(
|
| 131 |
process_with_char_set,
|
| 132 |
inputs=[image_input, width_slider, char_set_dropdown, custom_chars],
|
| 133 |
+
outputs=[output_text]
|
| 134 |
)
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|