Created
October 6, 2023 13:55
-
-
Save iCUE-Solutions/61eb889b785b7d7c02386275116368dd to your computer and use it in GitHub Desktop.
ASCII Art
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
from PIL import Image | |
import sys | |
from PIL import Image | |
# from termcolor import colored | |
ascii_variations = { | |
"variation1": ["B", "S", "#", "&", "@", "$", "%", "*", "!", ":", "."], | |
"variation2": ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"], | |
"variation3": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], | |
"variation4": ["Z", "Y", "X", "W", "V", "U", "T", "S", "R", "Q", "P"], | |
"variation5": ["@", "#", "$", "%", "&", "*", "(", ")", "_", "+", "="], | |
"variation6": ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A"], | |
"variation7": ["!", "@", "#", "$", "*", "^", "&", "(", ")", "<", ">"], | |
"variation8": ["1", "A", "2", "B", "3", "C", "4", "D", "5", "E", "6"], | |
"variation9": [":", ";", ",", ".", "<", ">", "/", "?", "[", "]", "{"], | |
"variation10": ["L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V"], | |
"variation11": ["1", "3", "5", "7", "9", "0", "-", "=", "!", "}", "|"], | |
"variation12": ["z", "x", "c", "v", "b", "n", "m", "a", "s", "d", "f"] | |
} | |
def ascii_art(char_set, img, name): | |
""" | |
This function takes an image and converts it into ASCII art. | |
It resizes the image, converts it to grayscale, and maps the pixel values to ASCII characters. | |
The resulting ASCII art is saved to a file with the given name. | |
Args: | |
img (PIL.Image): The input image to be converted to ASCII art. | |
name (str): The name of the file to save the ASCII art to. | |
Returns: | |
str: The resulting ASCII art as a string. | |
""" | |
# Get the width and height of the image | |
width, height = img.size | |
# Calculate the aspect ratio of the image | |
aspect_ratio = height / width | |
# Set the new width of the image to 120 pixels | |
new_width = 120 | |
# Calculate the new height of the image based on the aspect ratio and the new width | |
new_height = aspect_ratio * new_width * 0.55 | |
# Resize the image to the new dimensions | |
img = img.resize((new_width, int(new_height))) | |
# Convert the image to grayscale | |
img = img.convert("L") | |
# Get the pixel values of the image | |
pixels = img.getdata() | |
# Map the pixel values to ASCII characters | |
st.info(f"char_set is {char_set}") | |
chars = ascii_variations[char_set] #["B", "S", "#", "&", "@", "$", "%", "*", "!", ":", "."] | |
st.error(f"chars is {chars}") | |
new_pixels = [chars[pixel // 25] for pixel in pixels] | |
new_pixels = "".join(new_pixels) | |
# Split the ASCII characters into lines of the appropriate width | |
new_pixels_count = len(new_pixels) | |
ascii_image = [ | |
new_pixels[index : index + new_width] | |
for index in range(0, new_pixels_count, new_width) | |
] | |
ascii_image = "\n".join(ascii_image) | |
# Print the resulting ASCII art to the console | |
print(ascii_image) | |
# Save the ASCII art to a file | |
file = f"{name}.ascii.txt" | |
with open(file, "w") as f: | |
f.write(ascii_image) | |
st.success(f"saved art image to file as {file}") | |
# Return the resulting ASCII art as a string | |
return ascii_image | |
# | |
# Main | |
# | |
st.set_page_config( | |
page_title="ASCII Art Generator", | |
page_icon="🧊", | |
layout="wide", | |
initial_sidebar_state="collapsed", | |
# menu_items={ | |
# 'Get Help': 'https://www.extremelycoolapp.com/help', | |
# 'Report a bug': "https://www.extremelycoolapp.com/bug", | |
# 'About': "# This is a header. This is an *extremely* cool app!" | |
# } | |
) | |
# st.title("ASCII Art Generator") | |
st.title(" :tophat: ASCII Art") | |
#get rando | |
char_lst = st.selectbox("Select a character list", list(ascii_variations.keys())) | |
value = ascii_variations.get(char_lst) | |
st.info(f"you selected {char_lst} which is {value}") | |
# st.write(f"you selected {char_lst} which is {value}") | |
# get the image from the user | |
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]) | |
if uploaded_file is not None: | |
image = Image.open(uploaded_file) | |
st.image(image, caption=f"{uploaded_file.name}", use_column_width=True) | |
st.info(char_lst) | |
ASCII_IMG = ascii_art(char_lst, image, uploaded_file.name) | |
st.text(ASCII_IMG) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment