from PIL import Image import gradio as gr import numpy as np def convert_to_ascii(image, text_size): if isinstance(image, str): gimage = Image.open(image) elif isinstance(image, np.ndarray): gimage = Image.fromarray(image) else: raise ValueError("Unsupported input type. Please provide a file path or a NumPy array.") width, height = gimage.size aspect_ratio = height / width new_width = int(text_size) new_height = int(aspect_ratio * text_size * 0.5) resized_image = gimage.resize((new_width, new_height)) grayscale_image = resized_image.convert('L') ascii_chars = '█@&%#*░+=-:,.\/|][}{)(´„‟‚‛‘ ' # add more characters if you want ascii_image = '' for y in range(new_height): for x in range(new_width): pixel_value = grayscale_image.getpixel((x, y)) if pixel_value == 255: # make sure to use the GOD DAM transparency ascii_image += ' ' else: ascii_image += ascii_chars[int(pixel_value / 255 * (len(ascii_chars) - 1))] ascii_image += '\n' return ascii_image # Create an Interface with a title iface = gr.Interface( fn=convert_to_ascii, # Function to run inputs=["image","number"], # Input component (in this case, an image) outputs="text", # Output component (in this case, text) title="image-to-ascii (by peasoup)" ) # Function to generate HTML code for the output text with a copy button def copy_button(output_text): return f"{output_text}

" # Set custom post-processing for the output text iface.test_command = lambda output_text: copy_button(output_text) # Launch the interface iface.launch()