Create ASCII Art

by Feb 22, 2019

It’s amazing how versatile PowerShell is: with just a couple of lines of code, you can turn any photo and image into a piece of ASCII art. PowerShell simply loads the image, then scans it line by line and row by row, and replaces each pixel with an ASCII character, based on the pixel brightness.

Here is the function:

 function Convert-ImageToAsciiArt
{
  param(
    [Parameter(Mandatory)][String]
    $Path,
    
    [ValidateRange(20,20000)]
    [int]$MaxWidth=80,
    
    # character height:width ratio
    [float]$ratio = 1.5
  )

  # load drawing functionality
  Add-Type -AssemblyName System.Drawing
  
  # characters from dark to light
  $characters = '$#H&@*+;:-,. '.ToCharArray()
  $c = $characters.count
  
  # load image and get image size
  $image = [Drawing.Image]::FromFile($path)
  [int]$maxheight = $image.Height / ($image.Width / $maxwidth)/ $ratio
  
  # paint image on a bitmap with the desired size
  $bitmap = new-object Drawing.Bitmap($image,$maxwidth,$maxheight)
  
  
  # use a string builder to store the characters
  [System.Text.StringBuilder]$sb = ""
  
  # take each pixel line...
  for ([int]$y=0 $y -lt $bitmap.Height $y++){
    # take each pixel column...
    for ([int]$x=0 $x -lt $bitmap.Width $x++){
      # examine pixel
      $color = $bitmap.GetPixel($x,$y)
      $brightness = $color.GetBrightness()
      # choose the character that best matches the
      # pixel brightness
      [int]$offset = [Math]::Floor($brightness*$c)
      $ch = $characters[$offset]
      if (-not $ch){ $ch = $characters[-1] }      
      # add character to line
      $null = $sb.Append($ch)
    }
    # add a new line
    $null = $sb.AppendLine()
  }

  # clean up and return string
  $image.Dispose()
  $sb.ToString()
}

And this is how you can use it:

$Path = "C:\Users\Tobias\Desktop\Somepic.jpg"
$OutPath = "$env:temp\asciiart.txt"

Convert-ImageToAsciiArt -Path $Path -MaxWidth 150 |
  Set-Content -Path $OutPath -Encoding UTF8 


Invoke-Item -Path $OutPath 

Just make sure you adjust the paths in the code. The ASCII art opens in your default text editor. Make sure you disable line wrap, choose a fixed-width font and a small-enough font size!

                                                                                                    
                                                                                                                            
                                         ;@&&&&&&@&&&&@+-                                           
                                       :@&&&&&&&&&&&&&&&&&&:.                                       
                                     ,&&&&&&&&&&&&&&&&&&&&&&&&&&*                                   
                                  -@&&&&&&&&&&&HHHHHH&&&&&&&&&&&&&*                                 
                               :&&&&&&&&&&&&H&&&&&&&&&&&&&&&&&&&&&&&@                               
                              *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,                             
                             :&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&-                            
                            *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*                           
                           &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&                          
                          @&&&&&&&&H&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&-                        
                         &&&&&&&&H&&&@;:+&&&&&&&*-....,:@&&&&&&&&&&&&&&&&&&&@                       
                        &&&&&&&&H&&;........................*&&&&&&&H&&&&&&&&&.                     
                      .&&&&&&&&&H;...........................&&&&&&&&H&&&&&&&&&-                    
                     ;&&&&&&&&&&:............................@@&&&&&&H&&&&&&&&&&                    
                    &&&&&&&&&&&.+...........................-.;&&&H&&H&&&&&&&&&&@                   
                   *&H&&&&&&&&;;..............................+@+&&HH&&&&&&&&&&&&                   
                   ;&H&&&&&&&&................................@*.&&&H&&&&&&&&&&&&:                  
                   :&H&&&&&&&*...............................:*..&H&&&&&&&&&&&&&&@                  
                   ,&&H&&&@&@...................................,&&&&&&&&&&&&&&&&&,                 
                   ,&&&&&&:+....................................+*.;:&&&&&&&&&&&&&&                 
                   +&&&&&&............................................@*@+&&&&&&&&&,                
                   &&&&&&;............................................,-..-+&&&&&&&                 
                  :&&&&&@-...................................................:&&&&&,                
                  &&&&&&&.....................................................&&&&&-                
                  &&&&&&+.....................................................&&&&&,,               
                  :&&&&&;.....................................................&&&H&                 
                   &&&&&;.......................................,.,...........&&&&H&@               
                  *-&&&&:...*&&&&&&&&&&&&&@*,.............-*&&&&&&&&&&&&:.....@&&&&+                
                    &&&&:.+&&HHHHHHHHHHHHH&&&&..........+&&&&HHHHHHHHHHHHH*...+&&&&@                
                   ,###H*+;;:,...........-;*&&##########&&&*:..........:;*@:;*&###H:                
                   :##H*.......................#@;---+#;.......................@###.                
                  ,@###+............+HH&+.....-&......-*......;&H&*............&##:.                
                   ,&&#-...........HHH&+H;....@;......,#.....@HHH;H@...........HH&-.                
                   ..;#,...........HHHHHH+....#........H,....&HHHHH&..........-#&:-                 
                   ...H*...........-;;;;;....-H........;@....,;;;;;,..........;#&..                 
                   ...&#.....................#-.........#.....................&H*..                 
                   ...:H&........,.........-#@..........-#;..........,,......:#&,..                 
                    ...-,&###&+::-,,::+&###+......  ......-###H*:::,,::+@####,....                  
                    ..........,,,,,,,,...........   ............,,,,,,,,..........                  
                     ............................   ..............................                  
                     ............................   .............................                   
                      .....................,.....   ............................                    
                       ....................,.....   .......,...................                     
                         ..................,,,...   .......,.................                       
                          ......................,.  ..,......................                       
                          ........................,,,.......................                        
                          ..................................................                        
                           .................................................                        
                           .............,-..................--.............                         
                            ...............--,,-----:-,. --...............                          
                             ..................,,----,,...................                          
                              ...........................................                           
                               .........................................                            
                                ......................................                              
                                 ....................................                               
                                   ................................                                 
                                    ..............................                                  
                                     .,,,,,,,,,,,,,,,,,,,,,,,,,,                                    
                                   ,&...,,,,,,,,,,,,,,,,,,,,,,...@,                                 
                                &HHH......,,,,,,,,,,,,,,,,,,.....-HHH&                              
                            :HHHHHHH&.........,,,,,,,,,,,........HHHHHHHH;                          
                         *HHHHHHHHHHHH+........................*HHHHHHHHHHHH@                       
                      -HHHHHHHHHHHHHHHHH&;..................;&HHHHHHHHHHHHHHHHH,                    
                     HHHHHHHHHHHHHHHHHHHHHHHH&*;:---::;*&HHHHHHHHHHHHHHHHHHHHHHH&                   
                    HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH                  

And here’s a cool fine-tuning trick: in the function Convert-ImageToAsciiArt, take a look at $characters. It is a string with the characters used in your ASCII art, and the characters are listed in descending brightness. For a black and white drawing, you could as well try:

   $characters = [char]0x2588, ' '  

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!