ASCII ART!!! (-_-) [-_-]

Posted: July 21, 2012 in Kreeps
Tags: , , ,

A picture is a poem without words.
– Horace

A picture is worth a thousand words.
-Napoleon Bonaparte

If a picture (image) can worth so many words, why can’t it be made of words (mere ASCII character). Thus, to add little fun to my work i decided to write a program to turn any image into an ASCII art.

Technically making an image from standard symbols and characters is ascii art.

example :

          * 
         / 
       HH 
     SSSSSS
    SSSSSSSS
    S )))) S 
   SS -  - SS
  SSS o  o SSS
 SSSS  6   SSSS
  SSS  __  SSS
   SSS    SSS
      W   W
     WW  WWW
   WWWW  WWWW 
   WWWWWWWWWW
        XXXXXXXXX
      XXXXXXXXXXXXX
    XXXXXXXXXXXXXXXX 
   (__________)XXXXXX 
    ( ___  ___ XXXXXX
       o/   o   XXXXX 
    (  /        XXXXX
      /___)     XXXXX
   (             XXXX
  (     ____    ) XXX
   (               XX
    (          )    X
     (       )      *
       (    )      ***
                    *
1962 book, “Art Typing”

Developing a real ASCII art by typing in each character is really a talent. But for a coder its merely few line of code to convert any image into ASCII file.

This is how i did using openCV library:

  • Open the image and perform little Gaussian smoothing

Mat image = imread(“7.jpg”);
GaussianBlur(image,image, Size(5,5),0);

  • Convert image into Gray Image

Mat grayImage = Mat(image.size(), CV_32FC1);
cvtColor(image, grayImage, CV_BGR2GRAY);

  • for each pixel of image depending on the pixel value write into a text file an ascii character

ofstream f (“file.txt”);
for(int i =0 ; i<grayImage.rows; ++i)
{
for(int j=0;j<grayImage.cols;++j)
{
if(grayImage.at<uchar>(i,j)>230)
f<<“`”;
else if(grayImage.at<uchar>(i,j)>200)
f<<“‘”;
else if(grayImage.at<uchar>(i,j)>160)
f<<“;”;
else if(grayImage.at<uchar>(i,j)>120)
f<<“O”;
else if(grayImage.at<uchar>(i,j)>80)
f<<“8”;
else if(grayImage.at<uchar>(i,j)>40)
f<<“N”;
else
f<<“M”;
}
f<<“\n”;
}

here different characters are chosen to represent different brightness in the final output depending on the white space they have around them.

quality can be improved further by more conditional constraints to the pixel value or edge detection.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s