Create ASCII Art Text Banners in Python

Advertisement

Advertisement

Overview

Introduction

 _   _      _ _       _ _
| | | | ___| | | ___ | | |
| |_| |/ _ \ | |/ _ \| | |
|  _  |  __/ | | (_) |_|_|
|_| |_|\___|_|_|\___/(_|_)

ASCII art has a long history in the hacker culture. If you check out any Phrack article and there is almost guaranteed to be some form of ASCII art in there. In this example we are specifically talking about ASCII art fonts that can be used to make banner text for command-line applications, network services, documentation, web pages, etc.

FIGlet was born in the 90s and is a library for converting regular text in to different forms of ASCII art fonts. Check out their font database.

You can find a tool online that will convert your text in to a FIGlet font here: http://patorjk.com/software/taag You can use this if you just want to quickly convert and copy/paste the result. But, if you want to generate your own dynamic text or embed it in your own Python application, you can use the pyfiglet module! Keep reading to learn how to use it.

Another fun old tool that is used to generate ASCII art text is cowsay. It is used to create quote bubbles with a person saying something. By default it is a cow with a quote bubble but there are many other characters like Tux the penguin that can be used. Cowsay is not covered here, but it deserves a mentio if you are adding ASCII art to something.

 _______________
< Hello, world! >
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Install pyfiglet

Pyfiglet is the module that will convert regular strings in to ASCII art fonts. Simply use pip to install pyfiglet.

pip install pyfiglet

Use pyfiglet command-line tool

Pyfiglet comes with a command-line tool you can use if you don't need to use the Python library. Below we will explain using it in our own Python code. You can use the command-line tool to generate text or to list the available fonts.

# In your shell/command prompt
pyfiglet                # Print all options
pyfiglet --list_fonts   # List fonts
pyfiglet "Hello world!" # Generate text

Use pyfiglet in Python code

Here is the basic usage for converting text to ASCII art fonts.

# pip install pyfiglet
import pyfiglet

ascii_banner = pyfiglet.figlet_format("Hello!!")
print(ascii_banner)
# Example output:
 _   _      _ _       _ _
| | | | ___| | | ___ | | |
| |_| |/ _ \ | |/ _ \| | |
|  _  |  __/ | | (_) |_|_|
|_| |_|\___|_|_|\___/(_|_)

The example above uses the default font. There are a lot of fonts available. Run

pyfiglet --list_fonts
in your terminal to list the fonts, or look inside the fonts directory of the pyfiglet module. Check out the pyfiglet fonts directory on GitHub.

from pyfiglet import Figlet
custom_fig = Figlet(font='graffiti')
print(custom_fig.renderText('Hello!!'))
# Example output
  ___ ___         .__  .__        ._._.
 /   |   \   ____ |  | |  |   ____| | |
/    ~    \_/ __ \|  | |  |  /  _ \ | |
\    Y    /\  ___/|  |_|  |_(  <_> )|\|
 \___|_  /  \___  >____/____/\____/____
       \/       \/                 \/\/

Conclusion

After reading this, you should feel comfortable finding a font you like and generating an ASCII art banner using pyfiglet by incorporating the pyfiglet command-line application in to your shell scripts, or by using the pyfiglet module in Python code to enhance your application.

Reference links

Advertisement

Advertisement