Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily print ascii-art text? [closed]

I have a program that dumps a lot of output, and I want some of that output to really stand out. One way could be to render important text with ascii art, like this web service does for example:

 #    #   ##   #####  #    # # #    #  ####    #    #  #  #  #    # ##   # # ##   # #    #   #    # #    # #    # # #  # # # #  # #        # ## # ###### #####  #  # # # #  # # #  ###   ##  ## #    # #   #  #   ## # #   ## #    #   #    # #    # #    # #    # # #    #  ####   

other solutions could be colored or bold output. So how to do this sort of stuff easily in Python?

like image 753
static_rtti Avatar asked Mar 09 '12 11:03

static_rtti


People also ask

How do you write ASCII text in Python?

Use pyfiglet in Python code Here is the basic usage for converting text to ASCII art fonts. ascii_banner = pyfiglet. figlet_format("Hello!!") in your terminal to list the fonts, or look inside the fonts directory of the pyfiglet module.

What is Pyfiglet?

Pyfiglet is also a library that can be used in python code: from pyfiglet import Figlet. f = Figlet(font='slant') print f.renderText('text to render') pyfiglet also supports reading fonts from a zip archive.


2 Answers

  • 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 sys  from colorama import init init(strip=not sys.stdout.isatty()) # strip colors if stdout is redirected from termcolor import cprint  from pyfiglet import figlet_format  cprint(figlet_format('missile!', font='starwars'),        'yellow', 'on_red', attrs=['bold']) 

Example

$ python print-warning.py  

missile

 $ python print-warning.py | cat .___  ___.  __       _______.     _______. __   __       _______  __ |   \/   | |  |     /       |    /       ||  | |  |     |   ____||  | |  \  /  | |  |    |   (----`   |   (----`|  | |  |     |  |__   |  | |  |\/|  | |  |     \   \        \   \    |  | |  |     |   __|  |  | |  |  |  | |  | .----)   |   .----)   |   |  | |  `----.|  |____ |__| |__|  |__| |__| |_______/    |_______/    |__| |_______||_______|(__) 
like image 165
jfs Avatar answered Nov 02 '22 00:11

jfs


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])]) 
like image 36
jshepherd Avatar answered Nov 01 '22 23:11

jshepherd