├── OutConsolePicture.psd1 ├── OutConsolePicture.psm1 └── README.md /OutConsolePicture.psd1: -------------------------------------------------------------------------------- 1 | # 2 | # Module manifest for module 'PSGet_OutConsolePicture' 3 | # 4 | # Generated by: /u/NotNotWrongUsually 5 | # 6 | # Generated on: 14-03-2019 7 | # 8 | 9 | @{ 10 | 11 | # Script module or binary module file associated with this manifest. 12 | RootModule = 'OutConsolePicture.psm1' 13 | 14 | # Version number of this module. 15 | ModuleVersion = '1.6' 16 | 17 | # Supported PSEditions 18 | # CompatiblePSEditions = @() 19 | 20 | # ID used to uniquely identify this module 21 | GUID = 'b9c8316f-0408-4d3f-bb28-8a61d6ef679f' 22 | 23 | # Author of this module 24 | Author = '/u/NotNotWrongUsually' 25 | 26 | # Company or vendor of this module 27 | CompanyName = 'Unknown' 28 | 29 | # Copyright statement for this module 30 | Copyright = 'You can copy it, change it, or stick it in your hat; But never charge a penny for it - simple as that!' 31 | 32 | # Description of the functionality provided by this module 33 | Description = 'Out-ConsolePicture will take an image file and convert it to a text string. Colors will be "encoded" using ANSI escape strings. The final result will be output in the shell. By default images will be reformatted to the size of the current shell, though this behaviour can be suppressed with the -DoNotResize switch.' 34 | 35 | # Minimum version of the Windows PowerShell engine required by this module 36 | # PowerShellVersion = '' 37 | 38 | # Name of the Windows PowerShell host required by this module 39 | # PowerShellHostName = '' 40 | 41 | # Minimum version of the Windows PowerShell host required by this module 42 | # PowerShellHostVersion = '' 43 | 44 | # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. 45 | # DotNetFrameworkVersion = '' 46 | 47 | # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. 48 | # CLRVersion = '' 49 | 50 | # Processor architecture (None, X86, Amd64) required by this module 51 | # ProcessorArchitecture = '' 52 | 53 | # Modules that must be imported into the global environment prior to importing this module 54 | # RequiredModules = @() 55 | 56 | # Assemblies that must be loaded prior to importing this module 57 | # RequiredAssemblies = @() 58 | 59 | # Script files (.ps1) that are run in the caller's environment prior to importing this module. 60 | # ScriptsToProcess = @() 61 | 62 | # Type files (.ps1xml) to be loaded when importing this module 63 | # TypesToProcess = @() 64 | 65 | # Format files (.ps1xml) to be loaded when importing this module 66 | # FormatsToProcess = @() 67 | 68 | # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess 69 | # NestedModules = @() 70 | 71 | # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. 72 | FunctionsToExport = 'Out-ConsolePicture' 73 | 74 | # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. 75 | CmdletsToExport = @() 76 | 77 | # Variables to export from this module 78 | # VariablesToExport = @() 79 | 80 | # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. 81 | AliasesToExport = @() 82 | 83 | # DSC resources to export from this module 84 | # DscResourcesToExport = @() 85 | 86 | # List of all modules packaged with this module 87 | # ModuleList = @() 88 | 89 | # List of all files packaged with this module 90 | # FileList = @() 91 | 92 | # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. 93 | PrivateData = @{ 94 | 95 | PSData = @{ 96 | 97 | # Tags applied to this module. These help with module discovery in online galleries. 98 | Tags = 'Picture','Image','Console','ANSI', 'Graphics' 99 | 100 | # A URL to the license for this module. 101 | # LicenseUri = '' 102 | 103 | # A URL to the main website for this project. 104 | ProjectUri = 'https://github.com/NotNotWrongUsually/OutConsolePicture' 105 | 106 | # A URL to an icon representing this module. 107 | # IconUri = '' 108 | 109 | # ReleaseNotes of this module 110 | ReleaseNotes = 'https://github.com/NotNotWrongUsually/OutConsolePicture/releases' 111 | 112 | # External dependent modules of this module 113 | # ExternalModuleDependencies = '' 114 | 115 | } # End of PSData hashtable 116 | 117 | } # End of PrivateData hashtable 118 | 119 | # HelpInfo URI of this module 120 | # HelpInfoURI = '' 121 | 122 | # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. 123 | # DefaultCommandPrefix = '' 124 | 125 | } 126 | 127 | -------------------------------------------------------------------------------- /OutConsolePicture.psm1: -------------------------------------------------------------------------------- 1 | Add-Type -Assembly 'System.Drawing' 2 | 3 | function GetPixelText ($color_fg, $color_bg) { 4 | "`e[38;2;{0};{1};{2}m`e[48;2;{3};{4};{5}m" -f $color_fg.r, $color_fg.g, $color_fg.b, $color_bg.r, $color_bg.g, $color_bg.b + [char]9600 + "`e[0m" 5 | } 6 | 7 | function Out-ConsolePicture { 8 | [CmdletBinding()] 9 | param ( 10 | [Parameter(Mandatory = $true, ParameterSetName = "FromPath", Position = 0)] 11 | [ValidateNotNullOrEmpty()][string[]] 12 | $Path, 13 | 14 | [Parameter(Mandatory = $true, ParameterSetName = "FromWeb")] 15 | [System.Uri[]]$Url, 16 | 17 | [Parameter(Mandatory = $true, ParameterSetName = "FromPipeline", ValueFromPipeline = $true)] 18 | [System.Drawing.Bitmap[]]$InputObject, 19 | 20 | [Parameter()] 21 | [int]$Width, 22 | 23 | [Parameter()] 24 | [System.Drawing.Color]$TransparencyColor, 25 | 26 | [Parameter()] 27 | [ValidateSet("Left", "Center", "Right")] 28 | [string]$HorizontalPosition, 29 | 30 | [Parameter()] 31 | [switch]$DoNotResize 32 | ) 33 | 34 | begin { 35 | if ($PSCmdlet.ParameterSetName -eq "FromPath") { 36 | foreach ($file in $Path) { 37 | try { 38 | $image = New-Object System.Drawing.Bitmap -ArgumentList "$(Resolve-Path $file)" 39 | $InputObject += $image 40 | } 41 | catch { 42 | Write-Error "An error occurred while loading image. Supported formats are BMP, GIF, EXIF, JPG, PNG and TIFF." 43 | } 44 | } 45 | } 46 | 47 | if ($PSCmdlet.ParameterSetName -eq "FromWeb") { 48 | foreach ($uri in $Url) { 49 | try { 50 | $data = (Invoke-WebRequest $uri).RawContentStream 51 | } 52 | catch [Microsoft.PowerShell.Commands.HttpResponseException] { 53 | if ($_.Exception.Response.statuscode.value__ -eq 302) { 54 | $actual_location = $_.Exception.Response.Headers.Location.AbsoluteUri 55 | $data = (Invoke-WebRequest $actual_location).RawContentStream 56 | } 57 | else { 58 | throw $_ 59 | } 60 | } 61 | 62 | try { 63 | $image = New-Object System.Drawing.Bitmap -ArgumentList $data 64 | $InputObject += $image 65 | } 66 | catch { 67 | Write-Error "An error occurred while loading image. Supported formats are BMP, GIF, EXIF, JPG, PNG and TIFF." 68 | } 69 | } 70 | } 71 | 72 | if ($Host.Name -eq "Windows PowerShell ISE Host") { 73 | # ISE neither supports ANSI, nor reports back a width for resizing. 74 | Write-Warning "ISE does not support ANSI colors. No images for you. Sorry! :(" 75 | Break 76 | } 77 | } 78 | 79 | process { 80 | $InputObject | ForEach-Object { 81 | if ($_ -is [System.Drawing.Bitmap]) { 82 | 83 | # Do alpha blending if the image supports it 84 | if ([System.Drawing.Image]::IsAlphaPixelFormat($_.pixelformat)) { 85 | # Respect the transparency color if manually set 86 | if ($TransparencyColor) { 87 | $transparency_color = $TransparencyColor 88 | } else { 89 | # Default to using "One Half Dark" background 90 | $transparency_color = [System.Drawing.Color]::FromArgb(40, 44, 52) 91 | } 92 | # Create a blank bitmap, same size as our image. Colour it with the desired alpha colour 93 | # and then draw our image on top of it. Pass along the modified image. 94 | $base = New-Object System.Drawing.Bitmap -ArgumentList $_.Width, $_.Height 95 | $gfx_object = [System.Drawing.Graphics]::FromImage($base) 96 | $gfx_object.Clear($transparency_color) 97 | $gfx_object.DrawImage($_, 0, 0) 98 | $_ = $base 99 | } 100 | 101 | # Resize image to console width or width parameter 102 | if ($width -or (($_.Width -gt $host.UI.RawUI.WindowSize.Width) -and -not $DoNotResize)) { 103 | if ($width) { 104 | $new_width = $width 105 | } 106 | else { 107 | $new_width = $host.UI.RawUI.WindowSize.Width 108 | } 109 | $new_height = $_.Height / ($_.Width / $new_width) 110 | $resized_image = New-Object System.Drawing.Bitmap -ArgumentList $_, $new_width, $new_height 111 | $_.Dispose() 112 | $_ = $resized_image 113 | } 114 | 115 | # Extracting for performance purposes, so we don't have to access properties in a heavy loop later 116 | $img_width = $_.Width 117 | $img_height = $_.Height 118 | 119 | # Figure out where to place the image if positioning was specified 120 | switch ($HorizontalPosition) { 121 | "Left" { $pos_x = 0 } 122 | "Center" { $pos_x = [Math]::Floor(($host.UI.RawUI.WindowSize.Width - $img_width) / 2) } 123 | "Right" { $pos_x = $host.UI.RawUI.WindowSize.Width - $img_width } 124 | Default { $pos_x = 0 } 125 | } 126 | 127 | $color_string = New-Object System.Text.StringBuilder 128 | 129 | for ($y = 0; $y -lt $img_height; $y++) { 130 | if ($y % 2) { 131 | continue 132 | } 133 | else { 134 | [void]$color_string.append("`n`e[$($pos_x)G") 135 | } 136 | # If https://github.com/PowerShell/PowerShell/issues/8482 ever gets fixed, the below should return 137 | # to calling the GetPixelText function, like God intended. 138 | for ($x = 0; $x -lt $img_width; $x++) { 139 | if (($y + 2) -gt $img_height) { 140 | # We are now on the last row. The bottom half of it in images with uneven pixel height 141 | # should just be coloured like the background of the console. 142 | $color_fg = $_.GetPixel($x, $y) 143 | $color_bg = [System.Drawing.Color]::FromName($Host.UI.RawUI.BackgroundColor) 144 | $pixel = "`e[38;2;{0};{1};{2}m`e[48;2;{3};{4};{5}m" -f $color_fg.r, $color_fg.g, $color_fg.b, $color_bg.r, $color_bg.g, $color_bg.b + [char]9600 + "`e[0m" 145 | [void]$color_string.Append($pixel) 146 | } 147 | else { 148 | #$pixel = GetPixelText $_.GetPixel($x, $y) $_.GetPixel($x, $y + 1) 149 | $color_fg = $_.GetPixel($x, $y) 150 | $color_bg = $_.GetPixel($x, $y + 1) 151 | $pixel = "`e[38;2;{0};{1};{2}m`e[48;2;{3};{4};{5}m" -f $color_fg.r, $color_fg.g, $color_fg.b, $color_bg.r, $color_bg.g, $color_bg.b + [char]9600 + "`e[0m" 152 | [void]$color_string.Append($pixel) 153 | } 154 | } 155 | } 156 | $color_string.ToString() 157 | $_.Dispose() 158 | } 159 | } 160 | } 161 | 162 | end { 163 | } 164 | <# 165 | .SYNOPSIS 166 | Renders an image to the console 167 | .DESCRIPTION 168 | Out-ConsolePicture will take an image file and convert it to a text string. Colors will be "encoded" using ANSI escape strings. The final result will be output in the shell. By default images will be reformatted to the size of the current shell, though this behaviour can be suppressed with the -DoNotResize switch. ISE users, take note: ISE does not report a window width, and scaling fails as a result. I don't think there is anything I can do about that, so either use the -DoNotResize switch, or don't use ISE. 169 | .PARAMETER Path 170 | One or more paths to the image(s) to be rendered to the console. 171 | .PARAMETER Url 172 | One or more Urls for the image(s) to be rendered to the console. 173 | .PARAMETER InputObject 174 | A Bitmap object that will be rendered to the console. 175 | .PARAMETER DoNotResize 176 | By default, images will be resized to have their width match the current console width. Setting this switch disables that behaviour. 177 | .PARAMETER Width 178 | Renders the image at this specific width. Use of the width parameter overrides DoNotResize. 179 | .PARAMETER HorizontalPosition 180 | Takes the values "Left", "Center", or "Right" and renders the image in that position. 181 | .PARAMETER TransparencyColor 182 | If the image is transparent this is the color that will be used for transparency. The parameter needs a color object. Check examples for how to set it. This should be the same color as your console background usually. 183 | 184 | .EXAMPLE 185 | Out-ConsolePicture ".\someimage.jpg" 186 | Renders the image to console 187 | 188 | .EXAMPLE 189 | Out-ConsolePicture -Url "http://somewhere.com/image.jpg" 190 | Renders the image to console 191 | 192 | .EXAMPLE 193 | $image = New-Object System.Drawing.Bitmap -ArgumentList "C:\myimages\image.png" 194 | $image | Out-ConsolePicture 195 | Creates a new Bitmap object from a file on disk renders it to the console 196 | 197 | .EXAMPLE 198 | Out-ConsolePicture ".\someimage.jpg" -TransparencyColor ([System.Drawing.Color]::FromArgb(40, 44, 52)) 199 | Renders a transparent image using the specified color for transparency. 200 | 201 | .INPUTS 202 | One or more System.Drawing.Bitmap objects 203 | .OUTPUTS 204 | Gloriously coloured console output 205 | #> 206 | } 207 | 208 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OutConsolePicture 2 | Powershell cmdlet for rendering image files to console 3 | 4 | # How to install? 5 | The module is published to the Powershell Gallery, so get it from there with `Install-Module -Name OutConsolePicture` 6 | 7 | # Result 8 | ![Dandelion](https://i.imgur.com/80gucpA.png) 9 | 10 | # Documentation 11 | 12 | Straight from the module help: 13 | 14 | ``` 15 | NAME 16 | Out-ConsolePicture 17 | 18 | SYNOPSIS 19 | Renders an image to the console 20 | 21 | 22 | SYNTAX 23 | Out-ConsolePicture [-Path] [-Width ] [-TransparencyColor ] [-HorizontalPosition ] [-DoNotResize] [] 24 | 25 | Out-ConsolePicture -Url [-Width ] [-TransparencyColor ] [-HorizontalPosition ] [-DoNotResize] [] 26 | 27 | Out-ConsolePicture -InputObject [-Width ] [-TransparencyColor ] [-HorizontalPosition ] [-DoNotResize] [] 28 | 29 | 30 | DESCRIPTION 31 | Out-ConsolePicture will take an image file and convert it to a text string. Colors will be "encoded" using ANSI escape strings. The final result will be output in the shell. By 32 | default images will be reformatted to the size of the current shell, though this behaviour can be suppressed with the -DoNotResize switch. ISE users, take note: ISE does not report 33 | a window width, and scaling fails as a result. I don't think there is anything I can do about that, so either use the -DoNotResize switch, or don't use ISE. 34 | 35 | 36 | PARAMETERS 37 | -Path 38 | One or more paths to the image(s) to be rendered to the console. 39 | 40 | Required? true 41 | Position? 1 42 | Default value 43 | Accept pipeline input? false 44 | Accept wildcard characters? false 45 | 46 | -Url 47 | One or more Urls for the image(s) to be rendered to the console. 48 | 49 | Required? true 50 | Position? named 51 | Default value 52 | Accept pipeline input? false 53 | Accept wildcard characters? false 54 | 55 | -InputObject 56 | A Bitmap object that will be rendered to the console. 57 | 58 | Required? true 59 | Position? named 60 | Default value 61 | Accept pipeline input? true (ByValue) 62 | Accept wildcard characters? false 63 | 64 | -Width 65 | Renders the image at this specific width. Use of the width parameter overrides DoNotResize. 66 | 67 | Required? false 68 | Position? named 69 | Default value 0 70 | Accept pipeline input? false 71 | Accept wildcard characters? false 72 | 73 | -TransparencyColor 74 | If the image is transparent this is the color that will be used for transparency. The parameter needs a color object. Check examples for how to set it. This should be the same 75 | color as your console background usually. 76 | 77 | Required? false 78 | Position? named 79 | Default value 80 | Accept pipeline input? false 81 | Accept wildcard characters? false 82 | 83 | -HorizontalPosition 84 | Takes the values "Left", "Center", or "Right" and renders the image in that position. 85 | 86 | Required? false 87 | Position? named 88 | Default value 89 | Accept pipeline input? false 90 | Accept wildcard characters? false 91 | 92 | -DoNotResize [] 93 | By default, images will be resized to have their width match the current console width. Setting this switch disables that behaviour. 94 | 95 | Required? false 96 | Position? named 97 | Default value False 98 | Accept pipeline input? false 99 | Accept wildcard characters? false 100 | 101 | 102 | This cmdlet supports the common parameters: Verbose, Debug, 103 | ErrorAction, ErrorVariable, WarningAction, WarningVariable, 104 | OutBuffer, PipelineVariable, and OutVariable. For more information, see 105 | about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). 106 | 107 | INPUTS 108 | One or more System.Drawing.Bitmap objects 109 | 110 | 111 | OUTPUTS 112 | Gloriously coloured console output 113 | 114 | 115 | -------------------------- EXAMPLE 1 -------------------------- 116 | 117 | PS > Out-ConsolePicture ".\someimage.jpg" 118 | Renders the image to console 119 | 120 | 121 | 122 | 123 | 124 | 125 | -------------------------- EXAMPLE 2 -------------------------- 126 | 127 | PS > Out-ConsolePicture -Url "http://somewhere.com/image.jpg" 128 | Renders the image to console 129 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------- EXAMPLE 3 -------------------------- 136 | 137 | PS > $image = New-Object System.Drawing.Bitmap -ArgumentList "C:\myimages\image.png" 138 | $image | Out-ConsolePicture 139 | Creates a new Bitmap object from a file on disk renders it to the console 140 | 141 | 142 | 143 | 144 | 145 | 146 | -------------------------- EXAMPLE 4 -------------------------- 147 | 148 | PS > Out-ConsolePicture ".\someimage.jpg" -TransparencyColor ([System.Drawing.Color]::FromArgb(40, 44, 52)) 149 | Renders a transparent image using the specified color for transparency. 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | RELATED LINKS 158 | ``` 159 | 160 | # License 161 | You can copy it, change it, or stick it in your hat; 162 | But never charge a penny for it - simple as that! --------------------------------------------------------------------------------