ps1.soapyfrog.com

doing inappropriate things with powershell

Pages

  • About
  • Downloads
  • Archives

Search

Popular Posts

  • Grrr 1.1 and Big Invaders
  • Space Invaders
  • Convert images to text (ASCII art)
  • Cmdlet clashes
  • Console screen grabs in html

Recent Posts

  • Grrr source code, including Invaders
  • Google going down the pan
  • Cmdlet clashes
  • Grrr 1.1 and Big Invaders
  • Grrr, Cmdlets and PSInvaders revival

Categories

  • Announce (7)
  • Cmdlets (2)
  • Cool (16)
  • Grrr (6)
  • Hint (2)
  • Invaders (5)
  • Odd (2)
  • PowerShell (27)
  • Quiz (3)
  • Rant (7)
  • Uncategorized (1)
  • Utility (5)

Months

  • August 2007 (1)
  • April 2007 (1)
  • March 2007 (1)
  • February 2007 (3)
  • January 2007 (25)
  • December 2006 (1)

Bookmarks

  • Blogroll
    • $script Fanatics
    • blog.soapyfrog.com
    • Brian Long
    • Lee Holmes
    • Nik Crabtree
    • PowerShell-Scripting (French)
    • Richy Rich
    • The PowerShell Guy
    • Windows PowerShell
  • Links
    • Carbon-neutral web hosting!

Meta

  • Log in
  • Posts RSS
  • Comments RSS
  • Valid XHTML
  • Valid CSS
« Console screen grabs in html
Moving to WordPress »
 

Convert images to text (ASCII art)

07 Jan 2007 11:15 am// Cool, PowerShell, Utility    

Recently, whilst messing with sprites and the Grrr framework, I got thinking about better graphics for games.

As PowerShell is very slow at doing console atomic write operations, a game should really have fewer, larger sprites. I then thought about ASCII art, and wouldn’t it be great if PowerShell could convert existing images (jpg, png) and produce lines of text for use with Grrr’s create-image function.

So my morning’s work is a PowerShell script that does just that.

You can download it here: convert-image2text.ps1 (as a text file)

(If you’re reading this from the RSS feed, the link may be broken - open the blog page if so)

Unfortunately, it’s only monochrome. I had a lot of trouble getting a reasonable conversion of RGB or HSV to the console’s limited colour palette and character range, so in the end, I removed it.

The script uses System.Drawing and demonstrates one (a good?) way of adding an assembly to PowerShell.

Usage:

./convert-image2text.ps1 imagefile 
      [ -maxwidth nchars ] [-palette ascii | shade | bw ]

Here’s an example using the default -palette ascii option:

monolisa-ascii.gif

Here’s the same with the -palette shade option:

monolisa-shade.gif
Both are from this source image (click to see full size):


monalisa-small

6 comments to “Convert images to text (ASCII art)”

  1. On 08 Jan 2007 at 6:50 am, Rob Farley said:   

    The soapy frog does ASCII Art in PowerShell…

    Of course the idea is the same as Unix’s AALib , but what’s so cool is how little code it takes to do…

  2. On 08 Jan 2007 at 8:06 pm, ymasur said:   

    hmmm… I get an div by 0 error. The $imgwidth, forced gives nothing more. What type of image is supported? I tried a jpg one.
    Regards,
    //Yves

  3. On 08 Jan 2007 at 8:25 pm, adrian said:   

    @ymasur - i’ve used some png and jpg images.
    the image linked to the monalisa thumbnail for one.
    what image(s) are you using?

  4. On 09 Jan 2007 at 4:12 pm, Richy_Rich said:   

    I get this if you don’t put the explicit path to the input file. i.e.

    ./convert-image2text.ps1 blong.jpg
    fails with divide by zero error
    ./convert-image2text.ps1 c:\blong.jpg
    works

  5. On 09 Jan 2007 at 4:56 pm, adrian said:   

    Oh yes, so do I.

    Yet more weirdness, akin to what I was speaking about in gotcha 2 at http://ps1.soapyfrog.com/2007/01/09/text-and-path-gotchas/

    I’ll figure it out and repost the script.

  6. On 10 Jan 2007 at 11:05 am, adrian said:   

    I fixed it and updated the post to point to the new script.

    I needed to add this:
    $path=(resolve-path -erroraction “stop” $path).path

Copyright © 2006-2008 Adrian Milliner

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.