Bitmap Message

Tags: tiny, artistic, beginner

This program uses a multiline string as a bitmap, a 2D image with only two possible colors for each pixel, to determine how it should display a message from the user. In this bitmap, space characters represent an empty space, and all other characters are replaced by characters in the user’s message. The provided bitmap resembles a world map, but you can change this to any image you’d like. The binary simplicity of the space-or-message-characters system makes it good for beginners. Try experimenting with different messages to see what the results look like!

bitmap_message.py
 1"""Bitmap Message, by Al Sweigart al@inventwithpython.com
 2Displays a text message according to the provided bitmap image.
 3This code is available at https://nostarch.com/big-book-small-python-programming
 4Tags: tiny, beginner, artistic"""
 5
 6import sys
 7
 8# (!) Try changing this multiline string to any image you like:
 9
10# There are 68 periods along the top and bottom of this string:
11# (You can also copy and paste this string from
12# https://inventwithpython.com/bitmapworld.txt)
13bitmap = """
14....................................................................
15   **************   *  *** **  *      ******************************
16  ********************* ** ** *  * ****************************** *
17 **      *****************       ******************************
18          *************          **  * **** ** ************** *
19           *********            *******   **************** * *
20            ********           ***************************  *
21   *        * **** ***         *************** ******  ** *
22               ****  *         ***************   *** ***  *
23                 ******         *************    **   **  *
24                 ********        *************    *  ** ***
25                   ********         ********          * *** ****
26                   *********         ******  *        **** ** * **
27                   *********         ****** * *           *** *   *
28                     ******          ***** **             *****   *
29                     *****            **** *            ********
30                    *****             ****              *********
31                    ****              **                 *******   *
32                    ***                                       *    *
33                    **     *                    *
34...................................................................."""
35
36print('Bitmap Message, by Al Sweigart al@inventwithpython.com')
37print('Enter the message to display with the bitmap.')
38message = input('> ')
39if message == '':
40    sys.exit()
41
42# Loop over each line in the bitmap:
43for line in bitmap.splitlines():
44    # Loop over each character in the line:
45    for i, bit in enumerate(line):
46        if bit == ' ':
47            # Print an empty space since there's a space in the bitmap:
48            print(' ', end='')
49        else:
50            # Print a character from the message:
51            print(message[i % len(message)], end='')
52    print()  # Print a newline.

https://inventwithpython.com/bigbookpython/project3.html