PIL gives a cool way to do this very simple.
You can render the text onto a b/w image and convert that bitmap to a string stream replacing the black and white pixels to chars.
from PIL import Image, ImageFont, ImageDraw
ShowText = 'Python PIL'
font = ImageFont.truetype('arialbd.ttf', 15) #load the font
size = font.getsize(ShowText) #calc the size of text in pixels
image = Image.new('1', size, 1) #create a b/w image
draw = ImageDraw.Draw(image)
draw.text((0, 0), ShowText, font=font) #render the text to the bitmap
for rownum in range(size[1]):
#scan the bitmap:
# print ' ' for black pixel and
# print '#' for white one
line = []
for colnum in range(size[0]):
if image.getpixel((colnum, rownum)): line.append(' '),
else: line.append('#'),
print ''.join(line)
It renders the next result:
####### ## ####### ## ##
## ### ## ## ## ### ## ##
## ## ## ## ## ## ## ##
## ## ## ## #### ###### #### ###### ## ## ## ##
## ## ## ### ## ### ## ## ## ### ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
###### ## ## ## ## ## ## ## ## ## ###### ## ##
## ## # ## ## ## ## ## ## ## ## ## ##
## #### ## ## ## ## ## ## ## ## ## ##
## #### ## ## ## ## ## ## ## ## ## ##
## ## ### ## ## #### ## ## ## ## ########
##
##
###
##
###
I made a little more comprehensive example with functional style.
import Image, ImageFont, ImageDraw
ShowText = 'Python PIL'
font = ImageFont.truetype('arialbd.ttf', 15) #load the font
size = font.getsize(ShowText) #calc the size of text in pixels
image = Image.new('1', size, 1) #create a b/w image
draw = ImageDraw.Draw(image)
draw.text((0, 0), ShowText, font=font) #render the text to the bitmap
def mapBitToChar(im, col, row):
if im.getpixel((col, row)): return ' '
else: return '#'
for r in range(size[1]):
print ''.join([mapBitToChar(image, c, r) for c in range(size[0])])