Enter pip install opencv
in terminal to update / install OpenCV.
2 sample images are included by default
- Download the folder
- Open
script.py
and update the folder path toascii_img
(copy from file explorer) - Drag images into the
ascii_img folder
- Run the python script,
.txt
files will be created in the same folder - Open .txt files using Notepad (preferably)
- Ctrl + scroll to adjust size of image
Doug | ASCII Doug |
---|---|
![]() |
![]() |
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.