Created
May 2, 2014 03:30
-
-
Save gpduck/ba22ad28a344efeced25 to your computer and use it in GitHub Desktop.
ConvertTo-AsciiArt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.DESCRIPTION | |
Converts an image to ASCII art. | |
.PARAMETER Path | |
The path to an image file to convert to ASCII. This can be any image format supported by System.Drawing.Bitmap. | |
.PARAMETER Uri | |
The URI for an image to download and convert to ASCII. This can be any image format supported by System.Drawing.Bitmap. | |
.PARAMETER Bytes | |
A byte array representing an image to convert to ASCII. This can be any image format supported by System.Drawing.Bitmap. | |
.PARAMETER Invert | |
Invert the colors so they show up correctly on the PowerShell console (which usually has a dark background with light text). | |
.PARAMETER Width | |
The width to resize the image to. If width is specified but not height, then the image will be scaled proportionately. If neither width nor height are specified the image will be displayed at it's native resolution. | |
.PARAMETER Height | |
The height to resize the image to. If height is specified but not width, then the image will be scaled proportionately. If neither width nor height are specified the image will be displayed at it's native resolution. | |
.EXAMPLE | |
ConvertTo-AsciiArt -Uri http://bit.ly/1fUuN72 -Width 100 -Invert | |
Downloads an image from http://bit.ly/1fUuN72 and displays it in the console. | |
.EXAMPLE | |
Get-ChildItem -Path c:\Windows\Web -Include *.jpg,*.png -Recurse | Get-Random | ConvertTo-AsciiArt -Invert -Width 119 | |
Grabs a random image included with Windows and displays it in the console. Note the default console window is 120 characters, but the last one does not print so this sets the width to 119. | |
.LINK | |
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx | |
#> | |
function ConvertTo-AsciiArt { | |
param( | |
[Parameter(ParameterSetName="File",ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] | |
[Alias("Fullname")] | |
[string]$Path, | |
[Parameter(ParameterSetName="Bytes")] | |
[Byte[]]$Bytes, | |
[Parameter(ParameterSetName="Uri")] | |
[Uri]$Uri, | |
[switch]$Invert, | |
[int]$Width, | |
[int]$Height | |
) | |
begin { | |
$CharMap = "█▓▒░#+-. " | |
$WtoB = $BtoW.ToCharArray() | |
[Array]::Reverse($WtoB) | |
if($Invert) { | |
$CharMap = $CharMap.ToCharArray() | |
[Array]::Reverse($CharMap) | |
} | |
} | |
process { | |
switch($PsCmdlet.ParameterSetName) { | |
"File" { | |
$Bmp = New-Object System.Drawing.Bitmap($Path) | |
} | |
"Bytes" { | |
$MS = New-Object System.IO.MemoryStream(,$Bytes) | |
$Bmp = New-Object System.Drawing.Bitmap($MS) | |
$MS.Close() | |
} | |
"Uri" { | |
$Response = Invoke-WebRequest -Uri $Uri -UseBasicParsing | |
$MS = New-Object System.IO.MemoryStream(,$Response.Content) | |
$Bmp = New-Object System.Drawing.Bitmap($MS) | |
} | |
} | |
if($Width) { | |
if(!$Height) { | |
$Height = ($Width * $Bmp.Height) / $Bmp.Width | |
} | |
} | |
if($Height) { | |
if(!$Width) { | |
wb/h = a | |
$Width = ($Bmp.Width * $Height) / $Bmp.Height | |
} | |
} | |
if($Width -and $Height) { | |
$Bmp = New-Object System.Drawing.Bitmap($Bmp, $Width, $Height) | |
} | |
for($x = 0; $x -lt $Bmp.Height; $x += 2) { | |
$RowBuilder = New-Object Text.StringBuilder($Bmp.Width + 2) | |
for($y = 0; $y -lt $Bmp.Width; $y++) { | |
$TopBrightness = $Bmp.GetPixel($y, $x).GetBrightness() | |
if($x + 1 -lt $Bmp.Height) { | |
$BottomBrightness = $Bmp.GetPixel($y, $x + 1).GetBrightness() | |
$Brightness = ($TopBrightness + $BottomBrightness) / 2 | |
} else { | |
$Brightness = $TopBrightness | |
} | |
$Char = $CharMap[ [int](($CharMap.Length - 1) * $Brightness)] | |
$RowBuilder.Append($Char) > $Null | |
} | |
$RowBuilder.ToString() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment