File size: 1,531 Bytes
adaf989
0ae36ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import streamlit as st
from PIL import Image

# 1. App Configuration
st.set_page_config(page_title="ASCII Art Studio", page_icon="💻", layout="centered")

# 2. The Title & Description
st.title("💻 CLI Test: ASCII Art Studio")
st.write("Uploaded via Hugging Face CLI. Upload an image to see the matrix!")

# 3. Helper Function to convert pixels to ASCII
def image_to_ascii(image, new_width=100):
    # Resize image
    width, height = image.size
    aspect_ratio = height / width
    new_height = int(aspect_ratio * new_width * 0.55)
    img = image.resize((new_width, new_height))
    
    # Convert to grayscale
    img = img.convert("L")
    
    # Map pixels to ASCII chars
    pixels = img.getdata()
    chars = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
    new_pixels = [chars[pixel // 25] for pixel in pixels]
    new_pixels = "".join(new_pixels)
    
    # Format string
    ascii_image = "\n".join([new_pixels[index:(index + new_width)] for index in range(0, len(new_pixels), new_width)])
    return ascii_image

# 4. The Uploader Widget
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])

# 5. The Logic
if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption="Original Image", use_column_width=True)
    
    st.write("### Generating ASCII Art...")
    ascii_art = image_to_ascii(image)
    
    # Display inside a code block so it looks like code
    st.code(ascii_art, language="text")