Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
def image_to_ascii(image, width=100, chars="@%#*+=-:. "):
|
| 6 |
+
"""
|
| 7 |
+
Convert image to ASCII art
|
| 8 |
+
"""
|
| 9 |
+
# Convert image to grayscale
|
| 10 |
+
if image.mode != 'L':
|
| 11 |
+
image = image.convert('L')
|
| 12 |
+
|
| 13 |
+
# Resize image while maintaining aspect ratio
|
| 14 |
+
original_width, original_height = image.size
|
| 15 |
+
aspect_ratio = original_height / original_width
|
| 16 |
+
height = int(width * aspect_ratio * 0.55) # 0.55 to account for character aspect ratio
|
| 17 |
+
|
| 18 |
+
# Resize image
|
| 19 |
+
img_resized = image.resize((width, height))
|
| 20 |
+
|
| 21 |
+
# Convert to numpy array
|
| 22 |
+
pixels = np.array(img_resized)
|
| 23 |
+
|
| 24 |
+
# Normalize pixels to 0-1 range
|
| 25 |
+
pixels = pixels / 255.0
|
| 26 |
+
|
| 27 |
+
# Map pixels to ASCII characters
|
| 28 |
+
ascii_chars = list(chars)
|
| 29 |
+
num_chars = len(ascii_chars)
|
| 30 |
+
|
| 31 |
+
# Create ASCII art
|
| 32 |
+
ascii_art = []
|
| 33 |
+
for row in pixels:
|
| 34 |
+
line = ''.join(ascii_chars[min(int(pixel * num_chars), num_chars - 1)] for pixel in row)
|
| 35 |
+
ascii_art.append(line)
|
| 36 |
+
|
| 37 |
+
return '\n'.join(ascii_art)
|
| 38 |
+
|
| 39 |
+
def process_image(image, width, character_set):
|
| 40 |
+
"""
|
| 41 |
+
Process the uploaded image and convert to ASCII art
|
| 42 |
+
"""
|
| 43 |
+
if image is None:
|
| 44 |
+
return "Please upload an image first!"
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
# Define character sets
|
| 48 |
+
char_sets = {
|
| 49 |
+
"Detailed (dense)": "@%#*+=-:. ",
|
| 50 |
+
"Simple (sparse)": "@#*+-. ",
|
| 51 |
+
"Blocks": "█▓▒░ ",
|
| 52 |
+
"Inverted": " .:-=+*#%@",
|
| 53 |
+
"Custom": character_set
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
chars = char_sets.get(character_set, "@%#*+=-:. ")
|
| 57 |
+
|
| 58 |
+
ascii_result = image_to_ascii(image, width, chars)
|
| 59 |
+
return f"```\n{ascii_result}\n```"
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"Error processing image: {str(e)}"
|
| 62 |
+
|
| 63 |
+
# Create Gradio interface
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
+
gr.Markdown("# Image to ASCII Art Converter")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 70 |
+
width_slider = gr.Slider(minimum=50, maximum=200, value=100, step=10, label="ASCII Art Width")
|
| 71 |
+
char_set_dropdown = gr.Dropdown(
|
| 72 |
+
choices=["Detailed (dense)", "Simple (sparse)", "Blocks", "Inverted", "Custom"],
|
| 73 |
+
value="Detailed (dense)",
|
| 74 |
+
label="Character Set"
|
| 75 |
+
)
|
| 76 |
+
custom_chars = gr.Textbox(
|
| 77 |
+
value="@%#*+=-:. ",
|
| 78 |
+
label="Custom Character Set (used when 'Custom' is selected)",
|
| 79 |
+
placeholder="Enter characters from darkest to lightest"
|
| 80 |
+
)
|
| 81 |
+
submit_btn = gr.Button("Convert to ASCII")
|
| 82 |
+
|
| 83 |
+
with gr.Column():
|
| 84 |
+
output_text = gr.Textbox(
|
| 85 |
+
label="ASCII Art Result",
|
| 86 |
+
lines=20,
|
| 87 |
+
max_lines=50,
|
| 88 |
+
show_copy_button=True
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Update character set based on dropdown selection
|
| 92 |
+
def update_char_set(character_set, custom_chars):
|
| 93 |
+
char_sets = {
|
| 94 |
+
"Detailed (dense)": "@%#*+=-:. ",
|
| 95 |
+
"Simple (sparse)": "@#*+-. ",
|
| 96 |
+
"Blocks": "█▓▒░ ",
|
| 97 |
+
"Inverted": " .:-=+*#%@",
|
| 98 |
+
"Custom": custom_chars
|
| 99 |
+
}
|
| 100 |
+
return char_sets.get(character_set, "@%#*+=-:. ")
|
| 101 |
+
|
| 102 |
+
# Process function that uses the selected character set
|
| 103 |
+
def process_with_char_set(image, width, character_set, custom_chars):
|
| 104 |
+
char_sets = {
|
| 105 |
+
"Detailed (dense)": "@%#*+=-:. ",
|
| 106 |
+
"Simple (sparse)": "@#*+-. ",
|
| 107 |
+
"Blocks": "█▓▒░ ",
|
| 108 |
+
"Inverted": " .:-=+*#%@",
|
| 109 |
+
"Custom": custom_chars
|
| 110 |
+
}
|
| 111 |
+
chars = char_sets.get(character_set, "@%#*+=-:. ")
|
| 112 |
+
|
| 113 |
+
if image is None:
|
| 114 |
+
return "Please upload an image first!"
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
ascii_result = image_to_ascii(image, width, chars)
|
| 118 |
+
return f"```\n{ascii_result}\n```"
|
| 119 |
+
except Exception as e:
|
| 120 |
+
return f"Error processing image: {str(e)}"
|
| 121 |
+
|
| 122 |
+
# Connect the components
|
| 123 |
+
char_set_dropdown.change(
|
| 124 |
+
update_char_set,
|
| 125 |
+
inputs=[char_set_dropdown, custom_chars],
|
| 126 |
+
outputs=custom_chars
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
submit_btn.click(
|
| 130 |
+
process_with_char_set,
|
| 131 |
+
inputs=[image_input, width_slider, char_set_dropdown, custom_chars],
|
| 132 |
+
outputs=output_text
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
demo.launch()
|