Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Created July 21, 2016 10:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PrateekKumarSingh/f29d5480bec18530173a1fc3c35e9a65 to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/f29d5480bec18530173a1fc3c35e9a65 to your computer and use it in GitHub Desktop.
Function Write-Pixel
{
param(
[String] [parameter(mandatory=$true, Valuefrompipeline = $true)] $Path
)
Begin
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.drawing")
# Console Colors and their Hexadecimal values
$Colors = @{
'FF000000' = 'Black'
'FF000080' = 'DarkBlue'
'FF008000' = 'DarkGreen'
'FF008080' = 'DarkCyan'
'FF800000' = 'DarkRed'
'FF800080' = 'DarkMagenta'
'FF808000' = 'DarkYellow'
'FFC0C0C0' = 'Gray'
'FF808080' = 'DarkGray'
'FF0000FF' = 'Blue'
'FF00FF00' = 'Green'
'FF00FFFF' = 'Cyan'
'FFFF0000' = 'Red'
'FFFF00FF' = 'Magenta'
'FFFFFF00' = 'Yellow'
'FFFFFFFF' = 'White'
}
# Algorithm to calculate closest Console color (Only 16) to a color of Pixel
Function Get-ClosetConsoleColor($PixelColor)
{
$Differences = Foreach($item in $Colors.Keys)
{
''|select @{n='Color';e={$Item}},@{n='Diff';e={[math]::abs([convert]::ToInt32($Item,16) - [convert]::ToInt32($PixelColor,16))}}
}
($Differences |sort Diff)[0].color
}
}
Process
{
Foreach($item in $Path)
{
#Convert Image to BitMap
$BitMap = [System.Drawing.Bitmap]::FromFile((Get-Item $Item).fullname)
Foreach($y in (1..($BitMap.Height-1)))
{
Foreach($x in (1..($BitMap.Width-1)))
{
$Pixel = $BitMap.GetPixel($X,$Y)
$BackGround = $Colors.Item((Get-ClosetConsoleColor $Pixel.name))
Write-Host " " -NoNewline -BackgroundColor $BackGround
}
Write-Host "" # Blank write-host to Start the next row
}
}
}
end
{
}
}
#"JSnover.png" |Write-Pixel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment