Skip to content

Converts image to ASCII by mapping each pixel's luminosity to an ASCII matrix, then prints it out.

Notifications You must be signed in to change notification settings

agentblack6000/image-to-ascii

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

image2ASCII

Convert images to ASCII art, like this one!

La Joconde (Mona Lisa) in ASCII

This project is a submission to my first hackathon, Sonoma Hacks 3.0! See the project page

Project Overview

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:

  1. Computing the pixel matrix (a fancy way of saying go through each pixel in the image)
  2. Convert the RGB values(the color of the pixel) using some formula to ASCII characters
  3. Print the ASCII characters

That's it!

Run image2ASCII

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.

Project Files

main.py

def calculate_pixel_luminosity(red: int, green: int, blue: int) -> int:

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.

def compute_ascii_character_map(pixel_luminosity: int, maximum_luminosity: int) -> str:

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

def compute_pixel_matrix(image) -> list[list[str]]:

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.

def main() -> None:

Prints out the image in ASCII

About

Converts image to ASCII by mapping each pixel's luminosity to an ASCII matrix, then prints it out.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages