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!
When you run bitmapmessage.py, the output will look like this:
Bitmap Message, by Al Sweigart [email protected]
Enter the message to display with the bitmap.
> Hello!
Hello!Hello!Hello!Hello!Hello!Hello!Hello!Hello!Hello!Hello!Hello!He
lo!Hello!Hello l !He lo e llo!Hello!Hello!Hello!Hello!He
llo!Hello!Hello!Hello He lo H l !Hello!Hello!Hello!Hello!Hello H
el lo!Hello!Hello!He lo!Hello!Hello!Hello!Hello!Hel
o!Hello!Hello lo e lo!H ll !Hello!Hello!H l
!Hello!He llo!Hel Hello!Hello!Hell ! e
Hello!He ello!Hello!Hello!Hello!Hell H
l H llo! ell ello!Hello!Hell !Hello el o
lo!H l ello!Hello!Hell ell !He o
!Hello llo!Hello!Hel el He o
!Hello!H lo!Hello!Hell l !H llo
ello!Hel Hello!He H llo Hell
ello!Hell ello!H l Hell !H l o!
ello!Hell ello!H l o o!H l H
lo!Hel ello! el o!Hel H
lo!He llo! e llo!Hell
llo!H llo! llo!Hello
llo! ll lo!Hell e
llo l e
ll l H
Hello!Hello!Hello!Hello!Hello!Hello!Hello!Hello!Hello!Hello!Hello!He
Instead of individually typing each character of the world map pattern, you can copy and paste the whole thing from https://inventwithpython.com/bitmapworld.txt. 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.
1. """Bitmap Message, by Al Sweigart [email protected]
2. Displays a text message according to the provided bitmap image.
3. This code is available at https://nostarch.com/big-book-small-python-programming
4. Tags: tiny, beginner, artistic"""
5.
6. import 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)
13. bitmap = """
14. ....................................................................
15. ************** * *** ** * ******************************
16. ********************* ** ** * * ****************************** *
17. ** ***************** ******************************
18. ************* ** * **** ** ************** *
19. ********* ******* **************** * *
20. ******** *************************** *
21. * * **** *** *************** ****** ** *
22. **** * *************** *** *** *
23. ****** ************* ** ** *
24. ******** ************* * ** ***
25. ******** ******** * *** ****
26. ********* ****** * **** ** * **
27. ********* ****** * * *** * *
28. ****** ***** ** ***** *
29. ***** **** * ********
30. ***** **** *********
31. **** ** ******* *
32. *** * *
33. ** * *
34. ...................................................................."""
35.
36. print('Bitmap Message, by Al Sweigart [email protected]')
37. print('Enter the message to display with the bitmap.')
38. message = input('> ')
39. if message == '':
40. sys.exit()
41.
42. # Loop over each line in the bitmap:
43. for 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.
After entering the source code and running it a few times, try making experimental changes to it. You can change the string in bitmap
to create entirely new patterns.
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
bitmap
variable’s string?i
variable created on line 45 represent?print()
on line 52?