Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Convert an Image File to ASCII Art Using PowerShell

22 Sep 2009   #1
Crypto

Windows 7 Professional, Windows XP
 
 
Convert an Image File to ASCII Art Using PowerShell

Code:
 #------------------------------------------------------------------------------
# Copyright 2006 Adrian Milliner (ps1 at soapyfrog dot com)
# http://ps1.soapyfrog.com
#
# This work is licenced under the Creative Commons 
# Attribution-NonCommercial-ShareAlike 2.5 License. 
# To view a copy of this licence, visit 
# http://creativecommons.org/licenses/by-nc-sa/2.5/ 
# or send a letter to 
# Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
#------------------------------------------------------------------------------
 
#------------------------------------------------------------------------------
# This script loads the specified image and outputs an ascii version to the
# pipe, line by line.
#
param(
  [string]$path = $(throw "Supply an image path"),
  [int]$maxwidth,            # default is width of console
  [string]$palette="ascii",  # choose a palette, "ascii" or "shade"
  [float]$ratio = 1.5        # 1.5 means char height is 1.5 x width
  )
 
 
 
#------------------------------------------------------------------------------
# here we go
 
$palettes = @{
  "ascii" = " .,:;=|iI+hHOE#`$"
  "shade" = " " + [char]0x2591 + [char]0x2592 + [char]0x2593 + [char]0x2588
  "bw"    = " " + [char]0x2588
}
$c = $palettes[$palette]
if (-not $c) {
  write-warning "palette should be one of:  $($palettes.keys.GetEnumerator())"
  write-warning "defaulting to ascii"
  $c = $palettes.ascii
}
[char[]]$charpalette = $c.ToCharArray()
 
# we need the drawing assembly
$dllpath=(get-command "system.drawing.dll").definition
[Reflection.Assembly]::LoadFrom($dllpath) | out-null
# load the image
$image = [Drawing.Image]::FromFile($path)
if ($maxwidth -le 0) { [int]$maxwidth = $host.ui.rawui.WindowSize.Width - 1}
[int]$imgwidth = $image.Width
[int]$maxheight = $image.Height / ($imgwidth / $maxwidth) / $ratio
$bitmap = new-object Drawing.Bitmap ($image,$maxwidth,$maxheight)
[int]$bwidth = $bitmap.Width; [int]$bheight = $bitmap.Height
# draw it!
$cplen = $charpalette.count
for ([int]$y=0; $y -lt $bheight; $y++) {
  $line = ""
  for ([int]$x=0; $x -lt $bwidth; $x++) {
    $colour = $bitmap.GetPixel($x,$y)
    $bright = $colour.GetBrightness()
    [int]$offset = [Math]::Floor($bright*$cplen)
    $ch = $charpalette[$offset]
    if (-not $ch) { $ch = $charpalette[-1] } #overflow
    $line += $ch
  }
  $line
}

Usage:
Code:
./convert-image2text.ps1 imagefile [ -maxwidth nchars ] [-palette ascii | shade | bw ]
Full article > ps1.soapyfrog.com Convert images to text (ASCII art)




My System SpecsSystem Spec
Reply

Thread Tools


Similar help and support threads
Thread Forum
Use PowerShell to add a file path exclusion in MSE?
Is is possible to user PowerShell to add an exclusion path in MSE? I have about 1000 computers I need to push out this exclusion too, but since it is read only except for the SYSTEM user. I am unable to use a basic batch file by using REG ADD . I know PowerShell has cmdlets specifically for...
System Security
Macro to convert table to convert table into iCalendar file?
First off, yes I want an iCalendar vs an Outlook calendar file because, heresy of heresies, I prefer Thunderbird to Outlook. http://www.msofficeforums.com/images/smilies/smile.gif Moving on... My work doesn't offer an option to download the weekly schedule as a file, and my schedule changes so...
Microsoft Office
Powershell script to count file types
Hi all, forgive me if this is the wrong folder. I am googling scripts to find a PS script to count file types and files in several directory's on our network. I am a noob on powershell, and I'm trying to experiment. I have several directory's on my "Libraries" directory on my pc at work. I'm...
General Discussion
trying to restore acronis image fails, convert to vhd and boot?
for backups of my main OS (in my signature) I have acronis TIH create a single image full backup daily to a different drive. Weekly a single image backup is created and saved on a different drive. Because my SSDs are in RAID0, every few weeks I make a system image backup using Acronis (outside of...
Virtualization


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 13:15.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App