-
Notifications
You must be signed in to change notification settings - Fork 1
/
Convert-ImageToASCIIArt.psm1
executable file
·147 lines (121 loc) · 5.81 KB
/
Convert-ImageToASCIIArt.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function Convert-ImageToASCIIArt {
<#
.SYNOPSIS
Function to convert an image to ASCII art
.DESCRIPTION
The function Convert-ImageToAsciiArt takes an image file path and converts the image to ASCII art
The ASCII art is created by replacing each pixel in the image with an ASCII character based on the brightness of the pixel
The ASCII characters used are specified in the $chars variable, and their brightness is determined by the grayscale value of the original pixel
.EXAMPLE
Convert-ImageToAsciiArt -ImagePath "C:\path\to\image.jpg"
Example of converting image 'C:\path\to\image.jpg' to ASCII art
.EXAMPLE
Convert-ImageToAsciiArt -ImagePath "C:\path\to\image.jpg" -MaxWidth 80 -Contrast 75
Example of converting image 'C:\path\to\image.jpg' to ASCII art,
specifying the MaxWidth of ASCII art output to 80 instead of default value of your terminal width
and the Contrast to 75 instead of the default value of 50
.EXAMPLE
Convert-ImageToAsciiArt C:\path\to\image.jpg
Example of converting image 'C:\path\to\image.jpg' to ASCII art without using the 'ImagePath' flag
This flag is not needed if the image path is specified as the first value when calling the function
.EXAMPLE
Convert-ImageToAsciiArt -Path C:\path\to\image.jpg -Width 80 | Out-File -FilePath $PWD\ASCII-art.txt
Example of converting image 'C:\path\to\image.jpg' to ASCII art with a maximum width of 80
Then outputting the ASCII art to a text file with Out-File to the current directory
This example uses parameter aliases 'Path' and 'Width' instead of the full parameters 'ImagePath' and 'MaxWidth'
.EXAMPLE
Convert-ImageToAsciiArt -Image C:\path\to\image.jpg -Verbose
Example of converting image 'C:\path\to\image.jpg' to ASCII art using the Verbose flag
This will show the verbose output of what is being done
This example uses parameter aliases 'Image' instead of the full parameter 'ImagePath'
.LINK
https://github.com/ConnerWill/Convert-ImageToASCIIArt
.NOTES
Author: Conner.Will
#>
[CmdletBinding()]
Param (
[Parameter(
Mandatory=$true,
Position=0,
HelpMessage='Enter path to image file'
)]
[ValidateScript({
# Validate the input is an image by attempting to load the file as an image
Try {
New-Object System.Drawing.Bitmap($_)
$true
} Catch {
$false
}
}, ErrorMessage = "{0} is not an image"
)]
[Alias("Path","Image")]
[string]
# Specifies the path to the image file to be converted to ASCII art (Supported extensions: .jpg .jpeg .png .bmp .gif)
$ImagePath,
[Parameter(
HelpMessage='Enter maximum width of ASCII art output (Default: 120)'
)]
[Alias("Width")]
[int]
# Specifies the maximum width for the ASCII art output (Default: 120)
$MaxWidth = $Host.UI.RawUI.WindowSize.Width,
[Parameter(
HelpMessage='Enter contrast value [0-100] (Default: 50)'
)]
[ValidateRange(0,100)]
[int]
# Specifies the contrast value of ASCII art output [0-100] (Default: 50)
$Contrast = 50
)
# List of ASCII characters to use for the output.
$chars = @(' ', '.', ',', ':', ';', 'o', 'x', '%', '#', '@')
Write-Verbose -Message "Using image: '${ImagePath}' with a MaxWidth of '${MaxWidth}' and a Contrast value of '${Contrast}'"
# Load the image and resize it to a maximum width of $MaxWidth.
Write-Verbose -Message "Loading image '${ImagePath}' and resizing it to a maximum width of '${MaxWidth}'"
$image = [System.Drawing.Image]::FromFile($ImagePath)
$ratio = $MaxWidth / $image.Width
$newWidth = [int]($image.Width * $ratio)
$newHeight = [int]($image.Height * $ratio)
$resizedImage = $image.GetThumbnailImage($newWidth, $newHeight, $null, [System.IntPtr]::Zero)
# Convert each pixel in the image to an ASCII character based on its brightness.
Write-Verbose -Message "Converting each pixel in the image to an ASCII character based on its brightness"
$asciiChars = for ($y = 0; $y -lt $resizedImage.Height; $y++) {
$line = for ($x = 0; $x -lt $resizedImage.Width; $x++) {
$pixel = $resizedImage.GetPixel($x, $y)
$brightness = ([int]$pixel.R * 0.299 + [int]$pixel.G * 0.587 + [int]$pixel.B * 0.114) / 255
$charIndex = [int]($brightness * ($chars.Count - 1))
$chars[$charIndex]
}
[string]::Join('', $line)
}
# Apply the contrast parameter by replacing the ASCII characters with different characters based on their brightness.
Write-Verbose -Message "Applying the contrast parameter by replacing the ASCII characters with different characters based on their brightness"
$minCharIndex = 0
$maxCharIndex = $chars.Count - 1
$midCharIndex = [int](($minCharIndex + $maxCharIndex) / 2)
$contrastChars = for ($i = 0; $i -lt $chars.Count; $i++) {
$brightness = $i / ($chars.Count - 1)
if ($brightness -lt $Contrast / 200) {
$minCharIndex
}
elseif ($brightness -gt ($Contrast + 100) / 200) {
$maxCharIndex
}
else {
$midCharIndex
}
}
Write-Verbose -Message "Replacing characters from ASCII art" #, Removing empty lines from beginning and end"
$asciiChars = $asciiChars -replace "[{0}-{1}]" -f $minCharIndex, $maxCharIndex, $contrastChars
# Remove blank lines from the start of the text
# $asciiChars = $asciiChars -replace "(?m)^\s*`r?`n", ""
# Remove blank lines from the end of the text
# $asciiChars = $asciiChars -replace "`r?`n\s*$", ""
# Output the ASCII art.
Write-Verbose -Message "Outputting the ASCII art"
Write-Output $asciiChars
}
# TODO:
#