-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ps1
94 lines (67 loc) · 2.67 KB
/
main.ps1
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
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing.Bitmap")
#functions
function Get-Average-Value-Of-Pixel {
param(
[System.ValueType]$pixel
)
$red = $pixel | select -ExpandProperty R
$green = $pixel | select -ExpandProperty G
$blue = $pixel | select -ExpandProperty B
$alpha = $pixel | select -ExpandProperty A
($red + $green + $blue) / 3
}
function Compress-Height {
param(
[System.Drawing.Bitmap]$sourceBitmap
)
[System.Drawing.Bitmap]$stepBitmap = [System.Drawing.Bitmap](New-Object System.Drawing.Bitmap($sourceBitmap.Size.Width ,[int]($sourceBitmap.Size.Height/2)))
foreach ($y in (1..($sourceBitmap.Size.Height/2 -1 ))){
foreach ($x in (1..($sourceBitmap.Size.Width - 1))){
$stepBitmap.SetPixel($x,$y,$sourceBitmap.GetPixel($x,$y*2))
}
}
$stepBitmap
}
function Draw-Result {
param (
[System.Drawing.Bitmap]$newBitmap
)
[string]$resultImage
for ($x = 1; $x -lt $newBitmap.Size.Height; $x++) {
foreach ($y in (1..($newBitmap.Size.Width -1))) {
#$resultImage += " " + ([char]([int][math]::Round( (Get-Average-Value-Of-Pixel $newBitmap.GetPixel($y,$x)) / 255 * 17 + 32 )))
Write-Host ([char]([int][math]::Round( (Get-Average-Value-Of-Pixel $newBitmap.GetPixel($y,$x)) / 255 * 17 + 32 ))) -NoNewline
}
Write-Host ""
#$resultImage += "`n"
}
#$resultImage > obama.txt
}
function Change-Image-Size {
param (
[System.Drawing.Bitmap]$Bitmap
)
$consoleWidth = 211
[System.Drawing.Bitmap]$stepBitmap = Compress-Height -sourceBitmap $Bitmap
[int]$imageWidth = [int]$stepBitmap.Size.Width
[int]$imageHeight = [int]$stepBitmap.Size.Height
[int]$consoleHeight = $imageHeight
if ($imageWidth -gt $consolenWidth) {
$consoleHeight = [math]::Round(($consoleWidth / $imageWidth) * $imageHeight)
[System.Drawing.Bitmap]$newBitMap = [System.Drawing.Bitmap](New-Object System.Drawing.Bitmap($consoleWidth ,$consoleHeight))
$widthStep = [math]::floor($imageWidth / $consoleWidth)
$heightStep = [math]::floor($imageHeight / $consoleHeight)
foreach ($i in (1..($consoleHeight - 1))) {
foreach ($j in (1 ..($consoleWidth - 1))){
$newBitMap.SetPixel($j,$i,$stepBitmap.GetPixel($j * $widthStep,$i * $heightStep))
}
}
Draw-Result -newBitmap $newBitMap
} else {
Draw-Result -newBitmap $Bitmap
}
}
#"main"
$BitMap = [System.Drawing.Bitmap]::FromFile((Get-Item "$pwd\greyCat.jpg").FullName)
Change-Image-Size -Bitmap $BitMap