Skip to content

eggsacc/Python-ASCII-from-image

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

ASCII from image

Dependencies

Enter pip install opencv in terminal to update / install OpenCV.

How to use

2 sample images are included by default

  1. Download the folder
  2. Open script.py and update the folder path to ascii_img (copy from file explorer)
  3. Drag images into the ascii_img folder
  4. Run the python script, .txt files will be created in the same folder
  5. Open .txt files using Notepad (preferably)
  6. Ctrl + scroll to adjust size of image

Example

Doug ASCII Doug



How it works

Computers represent images as an array of numbers. A grayscale image is simply a 2D matrix of numbers that represent the brightness (0-255) of each pixel. An image in the RGB colorspace is slightly more complex and is a stack of red, green and blue pixel values in a 3D matrix.

RGB matrix (3D) Grayscale matrix (2D)



To convert an image to ASCII, we just need to convert it to grayscale and map the brightness of each pixel to a corresponding ASCII character. Different ASCII represents brightness by the amount of space it occupies. The character @ and # are dense and representes darker pixels, while , and ' are perceived as lighter pixels.

The OpenCV python package is a powerful compter vision & image processing library, widely used for real-time processes and includes a ton of features. It is used here to obtain the image matrices, convert it to grayscale and scale the image.

The ASCII characters list here is .,-~:;=!*#$@, from lightest to darkest. The full ASCII grayscale sequence is $@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,"^.. You can add more intermediate characters in the original list to make the output more refined.

FYI, OpenCV converts RGB to grayscale by taking the weighted average of the 3 color chanels of each pixel. The formula is
Gray = 0.299·Blue + 0.587·Green + 0.114·Red
This is also referred to as the luminosity method, which accounts for humans perception as we are more sensitive to the color green as compared to red or blue.

About

Generate ASCII images using a python script.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages