Spaces:
Sleeping
Sleeping
shrinivas-sn commited on
Commit ·
0ae36ae
1
Parent(s): 4db2b9c
Test a project
Browse files- app.py +43 -1
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,2 +1,44 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
# 1. App Configuration
|
| 5 |
+
st.set_page_config(page_title="ASCII Art Studio", page_icon="💻", layout="centered")
|
| 6 |
+
|
| 7 |
+
# 2. The Title & Description
|
| 8 |
+
st.title("💻 CLI Test: ASCII Art Studio")
|
| 9 |
+
st.write("Uploaded via Hugging Face CLI. Upload an image to see the matrix!")
|
| 10 |
+
|
| 11 |
+
# 3. Helper Function to convert pixels to ASCII
|
| 12 |
+
def image_to_ascii(image, new_width=100):
|
| 13 |
+
# Resize image
|
| 14 |
+
width, height = image.size
|
| 15 |
+
aspect_ratio = height / width
|
| 16 |
+
new_height = int(aspect_ratio * new_width * 0.55)
|
| 17 |
+
img = image.resize((new_width, new_height))
|
| 18 |
+
|
| 19 |
+
# Convert to grayscale
|
| 20 |
+
img = img.convert("L")
|
| 21 |
+
|
| 22 |
+
# Map pixels to ASCII chars
|
| 23 |
+
pixels = img.getdata()
|
| 24 |
+
chars = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
|
| 25 |
+
new_pixels = [chars[pixel // 25] for pixel in pixels]
|
| 26 |
+
new_pixels = "".join(new_pixels)
|
| 27 |
+
|
| 28 |
+
# Format string
|
| 29 |
+
ascii_image = "\n".join([new_pixels[index:(index + new_width)] for index in range(0, len(new_pixels), new_width)])
|
| 30 |
+
return ascii_image
|
| 31 |
+
|
| 32 |
+
# 4. The Uploader Widget
|
| 33 |
+
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])
|
| 34 |
+
|
| 35 |
+
# 5. The Logic
|
| 36 |
+
if uploaded_file is not None:
|
| 37 |
+
image = Image.open(uploaded_file)
|
| 38 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
| 39 |
+
|
| 40 |
+
st.write("### Generating ASCII Art...")
|
| 41 |
+
ascii_art = image_to_ascii(image)
|
| 42 |
+
|
| 43 |
+
# Display inside a code block so it looks like code
|
| 44 |
+
st.code(ascii_art, language="text")
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
streamlit
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
Pillow
|