Zuhaib

Generate ASCII Art with Python

FRIDAY JULY 08 2022 - 1 MIN

ASCII art is a form of art that was popularized in the early days of computing, where characters from the ASCII character set are used to create images. It’s a fun and creative way to use programming to create something visually interesting.

In this post, we will explore how to generate ASCII art using Python.

Setting Up the Environment

Before we start generating ASCII art, we need to set up our environment. We will be using a Python library called art which provides functionality for creating ASCII art.

To install art, simply run the following command in your terminal:

pip install art

Generating ASCII Art

Once we have art installed, we can start generating ASCII art. The library provides a variety of styles and fonts to choose from, and we can also create our own custom fonts.

Here’s a simple example that generates ASCII art of the word “Python” using the “slant” font:

from art import text2art art = text2art("Python", font='slant') print(art)

This code will generate the following ASCII art:

_____ _ ____ _ / ___/____ (_)___/ / / ___ (_)___ \__ \/ __ \/ / __ / / / _ \ / / __ \ ___/ / / / / / /_/ / /_/ __/ / / /_/ / /____/_/ /_/_/\__,_/\__,_/\___(_)_/\____/

Creating Custom Fonts

One of the really cool things about the art library is that we can create our own custom fonts. This allows us to create ASCII art that is unique and personalized.

Here’s an example of how to create a custom font:

from art import tprint, font_creator font = font_creator("myfont", r""" ___________________________________________________ / This is my custom font. It includes all letters \ | of the alphabet as well as some special characters. | \ Enjoy! / --------------------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || """) tprint("Hello, World!", font='myfont')

This code will create a custom font that includes all letters of the alphabet as well as some special characters. We can then use this font to generate ASCII art using the tprint function.


For suggestions and queries, just contact me.

Zuhaib Ahmad © 2024