Bitmap message in Python

Python programming language - logo

In this post, I continue to explore with a new example where I create a bitmap message in Python.

So, 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.

After that, 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!

The code

Now, the code for the bitmap message in Python. Try to change the image I put in the program with another one that you like or copy the following code.

import sys

# (!) Try changing this multiline string to any image you like

# There are 68 periods along the top and bottom of this string:
bitmap = """
....................................................................
   **************   *  *** **  *      ******************************
  ********************* ** ** *  * ****************************** *
 **      *****************       ******************************
          *************          **  * **** ** ************** *
           *********            *******   **************** * *
            ********           ***************************  *
   *        * **** ***         *************** ******  ** *
               ****  *         ***************   *** ***  *
                 ******         *************    **   **  *
                 ********        *************    *  ** ***
                   ********         ********          * *** ****
                   *********         ******  *        **** ** * **
                   *********         ****** * *           *** *   *
                     ******          ***** **             *****   *
                     *****            **** *            ********
                    *****             ****              *********
                    ****              **                 *******   *
                    ***                                       *    *
                    **     *                    *
...................................................................."""

print('Bitmap Message, by Al Sweigart al@inventwithpython.com')
print('Enter the message to display with the bitmap.')
message = input('> ')
if message == '':
    sys.exit()

# Loop over each line in the bitmap:
for line in bitmap.splitlines():
    # Loop over each character in the line:
    for i, bit in enumerate(line):
        if bit == ' ':
            # Print an empty space since there's a space in the bitmap:
            print(' ', end='')
        else:
            # Print a character from the message:
            print(message[i % len(message)], end='')
    print()  # Print a newline.
The output in Visual Studio Code - Bitmap message in Python
The output in Visual Studio Code

So, a line of 68 periods at the top and bottom of the pattern acts as a ruler to help you align it correctly. However, the program will still work if you make typos in the pattern.

The bitmap.splitlines() method call on line 43 returns a list of strings, each of which is a line in the multiline bitmap string. Using a multiline string makes the bitmap easier to edit into whatever pattern you like. The program fills in any non-space character in the pattern, which is why asterisks, periods, or any other character do the same thing.

The message[i % len(message)] code on line 51 causes the repetition of the text in message. As i increases from 0 to a number larger than len(message), the expression i % len(message) evaluates to 0 again. This causes message[i % len(message)] to repeat the characters in message as i increases.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.