ASCII Art with variable pixel squares...

After the recent ASCII art blogs by Omer and Raj, I thought I'd have a little play around as well. I prefer gray-scale ASCII art to color, and decided to follow Raj's approach (one difference in gray-scale palette: " `.~:;I=i+oO08#M"), but allow for variable pixel square sizes. Haven't seen Raj's code, but it looks like 1 pixel always maps to one character, although it's quite common in ASCII art for a set of pixels to map to one character. Let's see some code:

private static string grayscalepalette=" `.~:;I=i+oO08#M";

public static
 string GetGrayScaleHtml(Bitmap bm,int pixelsquare)
{
 
StringBuilder sb = new StringBuilder();
 
for(int y=0;y<bm.Height-pixelsquare;y+=pixelsquare)
 
{
   
for(int x=0;x<bm.Width-pixelsquare;x+=pixelsquare)
   
{
     
int luminationtotal=0;
     
int lumination=0;
     
for (int i=0;i<pixelsquare;i++)
      
{
       
for (int j=0;j<pixelsquare;j++)
       
{
         
Color c = bm.GetPixel(x+i,y+j);
         
lumination = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);
         
luminationtotal+=lumination;
       
}
      
}
     
sb.Append(GetGrayScaleChar((int)(luminationtotal/(pixelsquare*pixelsquare))));
    
}
    
sb.Append(Environment.NewLine);
 
}
  
return sb.ToString();
}

private static char GetGrayScaleChar(int lumination)
{
  
int index = (int)(((grayscalepalette.Length-1)/255.0) * lumination);
 
return grayscalepalette[grayscalepalette.Length-1-index];
}



As you can see, we now pass a pixelsquare parameter in, and its squared value maps to one ASCII character.

See the example ASCII art image below, using pixelsquare=3, so 3x3 pixels now map to one character.

                                        `````~:~```              
                                        ``:o8MMMO~`              
                                  ```````+MOI~.:O#.```           
                                  ``````I+I.`````0+```           
                                  ```.~::::~~~~.`=8```           
                                ```.~~:::::::::~~~I~````         
                                ``~~~::::::::::::~~~;~``         
                                `~~~::::::::::::::~~~:.`         
                             ```~~~::::::;;;;;:::::~~~.````      
                             ``.~::::::;;;;;;;;::::~~~~.```      
                             ``~~:::::;;;;;;;;;;::::~~~~```      
                             `.~:::::;;;;;;;;;;;::::~~~~.``      
                             `~~:::::;;;;;;;;;;;;:::~~~~~``      
                          ```.~~::::;;;;;;;;;;;;;:::~~~~~.`      
                          ```~~~:::;;;;;;;II;;;;;::~~~~~~.`      
                          ``.~~::::;;;;;IIIIII;;;:::~~~~~.`      
                          ``.~~::::;;;;IIIIIIII;;;::~~~~~~`      
                          ``~~~:::;;;;IIIIIIIII;;;::~~~~~~`      
                        ```.~~:::;;;;;IIIIIIIII;;;:::~~~~~`      
                        ```~:::::;;;;IIIIIIIIII;;;:::~~~~.`      
                        ``~~:::;;;;;;;;;;IIIIII;;;:::~~~~.`      
                        `~~~::;;;;;;;::::::;;II;;::::~~~~.`      
                        .~:::;;;;;::::;;;;::::;;;::::~~~~``      
                        ..~;:;;;;:::;IIIIII;::::;:::~~~~.``      
                     ````~.~;;;;;::;I======I;:~~::::~~~~.``      
                     ```=M=.:I;;;:;Iii=i++i=I;~~::::~~~~```      
                  ``````;+:~:II;;;=I.``..~i+=I:~~:::~~~~```      
                  ``````.~:;;I=;;II``.i#=``I+=;:~:::~~~.         
                   `````~:;IIIiIII``..I0I.``iiI:~:::~~~`         
                   ````.~;I===iII:``.~:::~``.+=:::::~~~````      
                   ````.:;I===+==.``~:;;::.``i=;:::~~~;````      
                  ````..~;I==ii==.`.:;;I;:~``==;:::~~:00```      
                     `..~:;I=i=I=~.~:;III;~.`=I::::~~i#+         
                     ``.~::;;I;I=;~~:;III;~.`iI::::~:8#:         
                     ```.~::::;I=i;::;II;:~.;=;:::::+#0`         
                     ```.:;;;:;Iio+I;;;;;:~~iI;;;::;8#+`         
                     ```~:;I;;;Iio0o=II;;:;+=I;;;::+##;`         
                     ```~;III;;Iio000o+ii++iII;;;::;0#``         
                     ```~;;IIIIIiO800Ooo++i=II;;:::;::``         
                     ``:=;;III=i0###80oo+ii=I;;;:::;::.`         
                     `~==i==i+O8088###8Oo+i=I;;;::;:::.`         
                     `I=ii+oO##0OO000000O+i=I;;;::::~:``         
                  ```:==ii+oO880OOOOOOoooo+=I;;::::::~``         
                  ``.I==ii+oO0000000Ooo+ii+iI;;::::::.``         
                  ``:===ii+oO00888880Oo+i==iI;:::;::.```         
                  ``I==ii++oO088##880Oo+i====;:::~.`````         
                  `~===ii++oO088###880o+i====;::~`````           
                  `:==iii++oO08####880O+ii==I;::.`````           
                  `~=iiii++oO08888800Oo+iiiiI;:~.``              
                  ``Iii++++oOO0000OOOOOo+I+OI;:~.``              
                  ```;ii++oooOOOOO0080Oi:~=0I::~```              
                     `~=+oOO0888####0i;~~~+O;::~```              
                     `~;.:Ii+oOOo+iI;:::~:#+;::~```              
                     `.i..~~~::;;;;;;;:::O0I;::~```              
                     ``ii:~~~::;;;;;;::I00iI;::~```              
                     ``:+oiI:::;;;;;;=O#0+I;;::~```              
                     ` `;+ooOo++++o0###O+=I;;:::.``              
                     `` `~ioOO08#####0ooi=I;;;:.```              
                    ````.`:=+oO000OOOOoi==III:.```````           
                  ````````~Ii+ooOooOOo++ii=;~.````````           
                  `````````=+i==i+++ooo+=;:~..````````           
                ``````````.;+o====i++i=I;:~~..````````           
                ``````````~~i++===iiIII;;:~~..```````            
                `````````~;::+oi===:;I;;;::~..```````            
             ```````````.:I:~=++i=:::I;;;::~..``````````         
             ```````````~I:~.:io+::I:;;;;::~..`````.````         
        `   ```````````.;:~~~.I+;:I;::;;;::~~.`````````` ``      
        ``````````````.:;~.~~~~::I;::~;;::::~.```.`````````      
        ```````````.```~~..~~~~::::::~:;::::~......````````      
        ``````````..````...~~~:~~:::::~;:::~~~..~~...````````    
        ``````````..```....~~~~~::::;:~::::::~.~~~...`````````   
        ``````.```..```....~~~~~:::::;::;;;;:~~::~....````````   
        `````...`...```...~~~~~~:::::;;;;;;::~~::~~~..````````   
        `````.......```...~~~~~:::::::;;;;:::::::::~~..```````   
        `````....~~.``....~~~~~:::::::;;;;::::::::::~~..`````` ` 
        `````.....~~``....~~~~~:::::::;;;;;:::;:::::::~..``````` 
        ``````....~~~.....~~~~~:::::::;;;;;:::;:::::::~..``````` 
        ```````...~~::...~~~~~~::::::;;;;;;::;::::;:::~~..`````` 
        ```````...~~::::~~~~~~:::::::;;;;;;::;:::;;;:::~..`````` 
        ````````...~~:::;::~::::::::::;;;;;;;;:::;;;:::~~.`.```` 
           ``````...~~::::;;::::::::;;;;;;;;;;:::;;;:::~~...```` 
          ````````..~~~~:::::;;;;;:;;;;;;;;;;I;::;;;:::~~..````` 
          `` ``````...~~~~~::::;;;;;;;;;;;;;;;;;;;;;;::~..`````` 
             ```````....~~~~~~:::::::::::::::::::::~~~...`````   
              ```````.......~~~~~~~~~~~~~~~~~~~~~~~....``````    
               ````````.........~~~~~~~~~~~~~.......`````````    
                ````````````...................````````````      
                ```````````````````````````````````````````      
                  `````````````````````````````````              
                  `````````````````````````````````              
                      ``    ```````````````` ``                  

Original image can be found here.

Enjoy toying around!

Edited, since I forgot to add the GetGrayScaleChar method.

188 Comments

Comments have been disabled for this content.