# 🖌️ psAsciiArt True Powershell

By
,
Powershell
,
Modules
Published 2023-03-14

Get psNetspell here
https://www.powershellgallery.com/packages/psAsciiArt/0.0.1

# 👴 History of ASCII ART

Here we go
Here we go

ASCII art is a form of digital art that uses characters from the ASCII character set to create images. ASCII stands for American Standard Code for Information Interchange, which is a character encoding standard that assigns a unique code to each character. The art form was first developed in the 1960s, when computer art was in its early stages. The limited number of characters available in the ASCII character set made it a challenging medium for artists, but it also encouraged creativity and experimentation. ASCII art became popular in the 1980s with the rise of personal computers and bulletin board systems (BBSs), which were online communities that used ASCII art to decorate their messages and forums.

ASCII art has a rich history and has been used in various ways over the years. In the early days of computing, ASCII art was used for practical purposes, such as creating graphics for technical manuals and documentation. However, it soon became a popular form of self-expression and creativity, and artists began using it to create digital art. ASCII art has also been used in gaming, where it has been used to create characters, maps, and other game elements. Today, ASCII art continues to be used by artists and enthusiasts alike, and it has become a part of internet culture. Its popularity is a testament to the enduring appeal of this unique and versatile art form.

# 🙄 So Powershell is about...

Powershell is a robust scripting language used for automation tasks in Windows environments. It provides an extensive set of cmdlets for carrying out system-level tasks, making it an excellent choice for IT professionals who want to streamline their workflows. A new Powershell module is being introduced on the Powershell Gallery, which will make creating ASCII art from images possible.

# 😕 What is this about?

The ConvertTo-AsciiArt function in the new module accepts a path to an image file, width, and height parameters, and produces ASCII art from the image. The function first loads the System.Drawing namespace, loads the image file using the FromFile method, resizes the image to fit the console, and then converts the image to ASCII art. It uses a series of if-else statements to assign ASCII characters to each pixel based on its brightness level. The resulting ASCII art is displayed in the console.

The ConvertTo-AsciiArtColor function is another function included in the module. Like the previous function, it accepts a path to an image file, width, and height parameters. The function resizes the image to fit the console and then converts each pixel to a corresponding ASCII character based on its brightness level, just like the previous function. However, it also sets the foreground color of each ASCII character based on the pixel color. This allows the function to create a more realistic representation of the image in the console.

# 🐇 Code behind the magic

Function ConvertTo-AsciiArt {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [string]$ImagePath,
        [Parameter(Mandatory=$false)]
        [int]$Width = 80,
        [Parameter(Mandatory=$false)]
        [int]$Height = 160
    )

    # Load the System.Drawing namespace
    Add-Type -AssemblyName System.Drawing

    # Load the image file
    $image = [System.Drawing.Image]::FromFile($ImagePath)

    # Set the width and height of the ASCII art
    $width = $Width
    $height = $Height

    # Resize the image to fit the console
    $aspectRatio = $image.Width / $image.Height
    $newWidth = $height * $aspectRatio
    $newHeight = $width / $aspectRatio
    if ($newWidth -gt $width) {
        $consoleWidth = $width
        $consoleHeight = $newHeight
    } else {
        $consoleWidth = $newWidth
        $consoleHeight = $height
    }
    $image = $image.GetThumbnailImage($consoleWidth, $consoleHeight, $null, [IntPtr]::Zero)

    # Convert the image to ASCII art
    $ascii = ""
    for ($y = 0; $y -lt $image.Height; $y++) {
        for ($x = 0; $x -lt $image.Width; $x++) {
            $pixel = [System.Drawing.Color]$image.GetPixel($x, $y)
            $brightness = ($pixel.R + $pixel.G + $pixel.B) / 3
            if ($brightness -ge 230) {
                $ascii += " "
            } elseif ($brightness -ge 200) {
                $ascii += "."
            } elseif ($brightness -ge 180) {
                $ascii += ":"
            } elseif ($brightness -ge 160) {
                $ascii += "-"
            } elseif ($brightness -ge 130) {
                $ascii += "="
            } elseif ($brightness -ge 100) {
                $ascii += "+"
            } elseif ($brightness -ge 70) {
                $ascii += "*"
            } elseif ($brightness -ge 50) {
                $ascii += "#"
            } elseif ($brightness -ge 30) {
                $ascii += "%"
            } else {
                $ascii += "@"
            }
        }
        $ascii += "`n"
    }

    # Output the ASCII art to the console
    Write-Host $ascii
}
#EndRegion '.\Public\ConvertTo-AsciiArt.ps1' 69
#Region '.\Public\ConvertTo-AsciiArtColor.ps1' 0
Function ConvertTo-AsciiArtColor {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [string]$ImagePath,
        [Parameter(Mandatory=$false)]
        [int]$Width = 80,
        [Parameter(Mandatory=$false)]
        [int]$Height = 160
    )

    # Load the System.Drawing namespace
    Add-Type -AssemblyName System.Drawing

    # Load the image file
    $image = [System.Drawing.Image]::FromFile($ImagePath)

    # Set the width and height of the ASCII art
    $width = $Width
    $height = $Height

    # Resize the image to fit the console
    $aspectRatio = $image.Width / $image.Height
    $newWidth = $height * $aspectRatio
    $newHeight = $width / $aspectRatio
    if ($newWidth -gt $width) {
        $consoleWidth = $width
        $consoleHeight = $newHeight
    } else {
        $consoleWidth = $newWidth
        $consoleHeight = $height
    }
    $image = $image.GetThumbnailImage($consoleWidth, $consoleHeight, $null, [IntPtr]::Zero)

    # Convert the image to ASCII art
    for ($y = 0; $y -lt $image.Height; $y++) {
        $line = ""
        for ($x = 0; $x -lt $image.Width; $x++) {
            $pixel = [System.Drawing.Color]$image.GetPixel($x, $y)
            $brightness = ($pixel.R + $pixel.G + $pixel.B) / 3
            if ($brightness -ge 230) {
                $line += " "
            } elseif ($brightness -ge 200) {
                $line += "."
            } elseif ($brightness -ge 180) {
                $line += ":"
            } elseif ($brightness -ge 160) {
                $line += "-"
            } elseif ($brightness -ge 130) {
                $line += "="
            } elseif ($brightness -ge 100) {
                $line += "+"
            } elseif ($brightness -ge 70) {
                $line += "*"
            } elseif ($brightness -ge 50) {
                $line += "#"
            } elseif ($brightness -ge 30) {
                $line += "%"
            } else {
                $line += "@"
            }

            # Set the foreground color based on the pixel color
            $color = [System.ConsoleColor]::Black
            if ($pixel.R -ge 128) { $color += 1 }
            if ($pixel.G -ge 128) { $color += 2 }
            if ($pixel.B -ge 128) { $color += 4 }
            Write-Host -NoNewline $line[-1] -ForegroundColor $color
        }
        Write-Host ""
    }
}

Hope that was some good bedtime reading
Hope that was some good bedtime reading

# Thanks for reading

Grab the module using the link below

Get psAsciiArt Here
https://www.powershellgallery.com/packages/psAsciiArt/0.0.1

Stay Safe
Stay Safe