Convert images to ASCII art, like this one!
This project is a submission to my first hackathon, Sonoma Hacks 3.0! See the project page
Image2ASCII is a command line application that converts images to ASCII art.
Converting images to ASCII is surprisingly simple, it can be done in 3 steps:
- Computing the pixel matrix (a fancy way of saying go through each pixel in the image)
- Convert the RGB values(the color of the pixel) using some formula to ASCII characters
- Print the ASCII characters
That's it!
Clone the repo, download the image of choice, and run the following command:
python main.py -f image_filepath
That's it. You will the the resulting image printed out in the terminal.
Calculates and returns the pixel luminosity by forming a weighted average to account for human perception, using this formula:
L = 0.2126 R + 0.7152 G + 0.0722 B
Since humans are more sensitive to green light, it is weighted the most heavily, followed by red, then blue.
Computes ASCII character based on the luminosity percentage, multiplied the length of the ASCII matrix, subtracted by 1 (to account for the index) to get the index of the character to map to.
map_index = ( (pixel_luminosity / maximum_luminosity) * length of ASCII matrix ) - 1
Takes a PIL.Image object, goes through each of the pixels, maps the pixel's RGB values to an ASCII character, storing in a 2D array.
Prints out the image in ASCII