How to easily print ascii-art text? [closed]
pyfiglet - pure Python implementation of http://www.figlet.org
pip install pyfiglet
termcolor - helper functions for ANSI color formatting
pip install termcolor
colorama - multiplatform support (Windows)
pip install colorama
import sysfrom colorama import initinit(strip=not sys.stdout.isatty()) # strip colors if stdout is redirectedfrom termcolor import cprint from pyfiglet import figlet_formatcprint(figlet_format('missile!', font='starwars'), 'yellow', 'on_red', attrs=['bold'])
Example
$ python print-warning.py
$ python print-warning.py | cat.___ ___. __ _______. _______. __ __ _______ __| \/ | | | / | / || | | | | ____|| || \ / | | | | (----` | (----`| | | | | |__ | || |\/| | | | \ \ \ \ | | | | | __| | || | | | | | .----) | .----) | | | | `----.| |____ |__||__| |__| |__| |_______/ |_______/ |__| |_______||_______|(__)
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, ImageDrawShowText = 'Python PIL'font = ImageFont.truetype('arialbd.ttf', 15) #load the fontsize = font.getsize(ShowText) #calc the size of text in pixelsimage = Image.new('1', size, 1) #create a b/w imagedraw = ImageDraw.Draw(image)draw.text((0, 0), ShowText, font=font) #render the text to the bitmapfor 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, ImageDrawShowText = 'Python PIL'font = ImageFont.truetype('arialbd.ttf', 15) #load the fontsize = font.getsize(ShowText) #calc the size of text in pixelsimage = Image.new('1', size, 1) #create a b/w imagedraw = ImageDraw.Draw(image)draw.text((0, 0), ShowText, font=font) #render the text to the bitmapdef 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])])
This is fun. I've figured out how to use PIL (the "Pillow" fork, of course) and Numpy to do this fully "vectorized", i.e. without loops:
text = "Hi there"from PIL import Image, ImageDraw, ImageFontimport numpy as npmyfont = ImageFont.truetype("verdanab.ttf", 12)size = myfont.getsize(text)img = Image.new("1",size,"black")draw = ImageDraw.Draw(img)draw.text((0, 0), text, "white", font=myfont)pixels = np.array(img, dtype=np.uint8)chars = np.array([' ','#'], dtype="U1")[pixels]strings = chars.view('U' + str(chars.shape[1])).flatten()print( "\n".join(strings))
## ## ## ## ## ## ## ## ## ## ## ## ## ## ##### ##### #### ## ## #### ## ## ## ## ## ## ## ## ##### ## ## ######## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ###### ## ###### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## # ## ## # ## ## ## ### ## ## #### ## ####