ascii_art – Make bitmaps from text

This module allows you to define simple images with ascii art instead of using a bitmap editor tool and working with binary file resources.

For example:

images = ‘’’
/.=(0,0,0) #=(255,255,255) %=(255,0,0)
..#..
.#.#.
#…#
..%..
..%..
‘’’

The first line starting with “/” maps ascii art characters to colors. In this case the ‘.’ becomes BLACK, ‘#’ becomes WHITE, and ‘%’ becomes RED. You can also use the color constant names from the ‘colors’ module instead of the RGB tuple.

The lines that follow define a simple 5x5 pixel image that can be copied into a window for a background or used as the image of a sprite.

Here is a complex example demonstrating all the art features:

images = ‘’’
/.=TRANSPARENT #=WHITE %=RED
..#.. | /0y..
.#.#. | …..
#…# | …..
..%.. | …..
..%.. | …..
_____________
/%=GREEN
..#.. | /0x..
.#… | …..
#..%% | …..
.#… | …..
..#.. | …..
‘’’

Multiple images can be drawn in the same ascii art string. Images on a row are separated by a space ‘ ‘. Rows of images are separated by a blank line. The ‘|’ and ‘_’ characters are optional; they are ignored. You can use them for added visual separation.

Rows of images do not have to be the same length as they are here. One row can have more images than another row.

All the images on a single row must be the same width and height. Different rows can be different width/height.

The first line of each row can change the color mapping. The new values remain in effect on future rows unless changed by a later row. In this example, the ‘%’ is mapped to GREEN in all images on the second row.

If the upper left corner of an image is ‘/’, then the image is a mirror of another image. On the first row, the second image begins with ‘/0y’. This indicates that the image is a copy of image ‘0’ on that row (the first image on the row) but is mirrored on the Y axis.

The second image on the second row is a copy of image 0 (the first) image on that row but mirrored on the X axis.

You can mirror both directions at once. For instance ‘/0xy’.

ascii_art.get_images(text, transparent_color=(247, 251, 247), scale=1, color_map=None)[source]

Convert a sheet of ascii art images into a list of Bitmaps

Args

text (str): The ascii-art string. transparent_color (tuple, optional): The color value to treat as transparent. Default is colors.TRANSPARENT. scale (int): Pixel scaling factor color_map (dict, optional): Mapping of characters to colors

Returns

The created Bitmap objects.

Return type

list

Download Source