Convert images to text (ASCII art)

Home  >>  Cool  >>  Convert images to text (ASCII art)

Convert images to text (ASCII art)

On January 7, 2007, Posted by , In Cool,PowerShell,Utility, With 6 Comments

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.


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

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


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

Both are from this source image (click to see full size):


6 Comments so far:

  1. Rob Farley says:

    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. ymasur says:

    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.

  3. adrian says:

    @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. Richy_Rich says:

    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

  5. adrian says:

    Oh yes, so do I.

    Yet more weirdness, akin to what I was speaking about in gotcha 2 at

    I’ll figure it out and repost the script.

  6. adrian says:

    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