2012-08-31

ActionScript Ascii Art

Yesterday I found this pretty awesome tool that lets you convert any image to ascii art. It's basically done through traversing each pixel in the picture and converting it into a grayscale value and then replacing that grayscale value with an appropriate ascii-character, which one depends on how much black that pixel is.

Most important code part:

rgbVal = _data.getPixel(x, y);
redVal = (rgbVal & 0xFF0000) >> 16;
greenVal = (rgbVal & 0x00FF00) >> 8;
blueVal = rgbVal & 0x0000FF;

/*
* Calculate the gray value of the pixel.
* The formula for grayscale conversion: (Y = gray): Y = 0.3*R + 0.59*G + 0.11*B
*/
grayVal = Math.floor(0.3 * redVal + 0.59 * greenVal + 0.11 * blueVal);

It then continues with checking that value against a white and black treshold and then checking up the final gray-value against a "palette" containing:

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

index = Math.floor(grayVal / 4);
result += palette.charAt(index);

End result:

Say hello to my collegue, transformed into Ascii





Inga kommentarer:

Skicka en kommentar