Colorful ASCII-Art from Images

by Mar 11, 2019

In the previous tip we showed you how you can take any image or photo and turn it into a black&white ASCII art. Today, we have a revised Convert-ImageToAsciiArt function for you: it takes an image and converts it to colored ASCII art!

The pixel brightness is turned into an appropriate ASCII character, and the pixel color is applied to that character. The ASCII art is written to a HTML file since HTML is the easiest file format for colored text.

function Convert-ImageToAsciiArt
{
  param(
    [Parameter(Mandatory)][String]
    $ImagePath,
    
    [Parameter(Mandatory)][String]
    $OutputHtmlPath,
    
    [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($ImagePath)
  [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 = "<html><building style='font-family:""Consolas""'>"
  
  
  # take each pixel line...
  for ([int]$y=0 $y -lt $bitmap.Height $y++) {
    # take each pixel column...
    $null = $sb.Append("<nobr>")
    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] }
      $col = "#{0:x2}{1:x2}{2:x2}" -f $color.r, $color.g, $color.b
      if ($ch -eq ' ') { $ch = "&nbsp;"}
      $null = $sb.Append( "<span style=""color:$col""; ""white-space: nowrap;"">$ch</span>")
    }
    # add a new line
    $null = $sb.AppendLine("</nobr><br/>")
  }

  # close html document
  $null = $sb.AppendLine("</building></html>")
  
  # clean up and return string
  $image.Dispose()
  
  Set-Content -Path $OutputHtmlPath -Value $sb.ToString() -Encoding UTF8
}

And this is how you can turn an image into a beautiful set of ASCII art, display it in your browser, and even print it to your color printer:

$ImagePath = "C:\someInputPicture.jpg"
$OutPath = "$home\desktop\ASCIIArt.htm"

Convert-ImageToAsciiArt -ImagePath $ImagePath -OutputHtml $OutPath -MaxWidth 150 
Invoke-Item -Path $OutPath

By adjusting -MaxWidth you can control the details. If you increase the width, you should also play with the font size and decrease the characters. For smaller characters, you might adjust this line:

[System.Text.StringBuilder]$sb = "<html><building style='font-family:""Consolas""'>"

Change it to this line, for example:

   [System.Text.StringBuilder]$sb = "<html><building style='font-family:""Consolas"";font-size:4px'>"


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!