11 January 2013
Category:
Unsorted
Comments: 2

Make ASCII Art with InDesign

Do you like ASCII Art ? Me too ! A few weeks ago, I have been started thinking if it could be possible to generate ASCII Art from any regular InDesign Content. The concept is easy : take an image and translate every pixel into a representative character. How could I possibly introduce that idea within InDesign ? Finally, I succeeded in getting this done. Not only I can convert images but any kind of content into ASCII Art.

Thanks to an extension, you can now generate ASCII Art from images but also shapes, text frames, groups and so forth. Is it possible the render isn’t contrasted enough ? You can boost the contrast to get a better result ? Is the result too large, reduce the scale of the output ! Do you want to use your own character set ? Well, just edit the characters associated to the tonal ranges. Do you want to keep the underlying material or remove it ? Just ask it !

If you ever wanted a cool and easy way to play with ASCII Art within InDesign, download the extension and free your mind :D

asciiMe UI

It’s hard to state when ASCII Art appeared. If we only considered the computer side, then ASCII Art is quite recent. If we are more open minded and consider ASCII Art as playing with characters and typography, then the history is quite older. I won’t dare giving a date myself. But let’s stick to a simple definition of ASCII Art as a way to express ideas and images by ordering characters in a certain way.

So the question is how to we play ourselves in order to introduce ASCII Art within InDesign ? The answer is called BitmapData. BitmapData is a class that give access to the raw structure of an image. So once you initiated this class, you can now read informations like width, height and every pixel composition. In my development, I envisioned two leads. One was pure extendscript using the Adobe Bridge DOM and the other was using the AS3 BitmapData class. As I am delivering an extension, you can understand that I picked the second one. I will introduce both approaches and then speak a little about the product.

A ) BitmapData in Adobe Bridge

Adobe Bridge offers an access to an image BitmapDatas though its DOM. Given a selection in Bridge, the following snippet reveals the RGB value of the first pixel of the image.


var th = app.document.selections[0];
var bmd = new BitmapData ( File ( th.path ));
var pix1 = bmd.getPixel(0,0);
var clr = new Color (pix1);
alert( "Red:"+clr.red+"\rGreen:"+clr.green+"\rBlue:"+clr.blue);

asciiMe Bridge Result

Is this information correct ? Let see what Photoshop tells us:

<

asciiMe Photoshop Result

It seems like we have a gap between what bridge returns and the effective value. I think that Bridge reads the raw value when ICC profile in Photoshop modifies the rendering of the pixel. But as ASCII Art isn’t that much about accuracy but expressionism, we can afford this gap.

So Bridge is able to look at the pixels value. Once that said, we can now envision to get our ASCII Art script done. But as the result has to be outputted in InDesign, we need to create a bridge between the two apps. But I will stop here for a few reasons. I indeed went a bit further but what made me change my mind was :

  1. Performance was terrible
  2. Too many technologies implied.

Could AS3 offers more opportunities ?

B )  BitmapData in AS3

BitmapData in AS3 is not much different than Bridge’s. Of course, the methods and properties change but the idea of examining pixels value remain. Let’s see how we can get the pixel 1 value in AS3. The following snippet will let us pick a file and learn about teh RGB value of the first pixel as we did in Bridge scripting:


protected function button1_clickHandler(event:MouseEvent):void
{
 var f:File = new File();
 f.browseForOpen("open");
 f.addEventListener(Event.SELECT, fileSelected );
}

private function fileSelected ( e:Event ):void
{
 var f:File = e.target as File;

 var bitmapData:BitmapData;
 var loader:Loader = new Loader();
 loader.contentLoaderInfo.addEventListener ( flash.events.Event.COMPLETE, onComplete );
 loader.load ( new URLRequest ( f.url ) );
}

private function onComplete (event:Event ):void
{
 var bmd:BitmapData = Bitmap( LoaderInfo ( event.target).content).bitmapData;
 var pix1:uint = bmd.getPixel(0,0);
 var rgb:Object = HexToRGB(pix1);
 Alert.show( "Red:"+rgb.r+"\rGreen:"+rgb.g+"\rBlue:"+rgb.b );
}

private function HexToRGB(value:uint):Object {
 var rgb:Object = new Object();
 rgb.r = (value >> 16) & 0xFF
 rgb.g = (value >> 8) & 0xFF
 rgb.b = value & 0xFF
 return rgb;
}

The last function ( HexToRGB ) comes from this link : http://snipplr.com/view.php?codeview&id=48683

And what about the result ?

asciiMe Air Result

Once again, the result differs from Photoshop and Bridge. If we can understand that ICC profiles may affect the RGB values in Photoshop, it’s more suprising to have gaps between AS3 and ExtendScript. Well, I think it’s quite understandable. ExtendScript has its own cooking and we are getting the RGB value from another calculation. Don’t forget that pixel values are indeed numbers and their translation may leed to some calculation leaks and in fine to a gap. Once again, we do not care here about absolute precision.

C) The « Ascii Me » extension

More than « Ascii-ing » content within InDesign, my idea was to give control over a few parameters.Characters for instance. It was obvious for me that we can’t force people to use a closed set of characters. Creativity comes when user have free hands over processus. Contrast also because some pictures or objects may be too low contrasted to generate a great render. Scaling the render was important to gain in performance and object handling.

Instructions :

  1. Prepare some paragraph style to control the final look ( not a prerequisite but it will ease your process) :

– Set a monospace font

– Equals font size and leading

– Dramatically increase kerning to get a more natural proportion

  1. Select one or more object to ASCII
  2. Edit parameters if needed
  3. Click on the « Ascii Me » button

Once that done, the doors of your creativity are wide open ! Here are a few renders I could generate myself :

asciiMe Result 1

D) Conclusion

It’s no mistery that I do like Extensions a lot. The reason on top of that is that you can create convergence between two universes and get the best of each of them : AS3 and ExtendScript. This Ascii Me extension shows this possible gathering. At some point in my development, I really thought I was exploring virgin lands when I finally found this link :

http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7eee.html

The sample shows another approach. For example, the tonal range is much wider than mine. I have 5 ranges, they have 64 of them.

private static const palette:String = <code>"@#$%&8BMW*mwqpdbkhaoQ0OZXYUJCLtfjzxnuvcr[]{}1()|/?Il!i><+_~-;,. "

And for each of these ranges, tehre is a character associated. I was achieving my development when I think I could use these 64 values for this extension but I finally went back to my 5 ranges as the result wasn’t the one I had in mind. Sometimes the best is the enemy of good. I was ok with my ranges and I didn’t feel like the 64 values really improve the product. But of course, that’s my humble opinion.

Hope you have fun and if you do something great, feel free to send me so I can post it here or place a link in the comments.

Enjoy and if you really like this, look at the Paypal button ;)

Loic

Download the AsciiMe Extension

2 responses on “Make ASCII Art with InDesign

  1. Jakob says:

    How do I install this unsigned extension. Because I get a warning and then it says it didn’t instal, because it is no signed extention. Please help!

Leave a Reply

Your email address will not be published. Required fields are marked *