Skip to main content

A Python module for displaying bitmaps in the terminal as strings of block characters.

Project description

tbmp - A data structure for terminal-based bitmaps.

By Al Sweigart al@inventwithpython.com

A Python module for displaying bitmaps in the terminal as strings of block characters. Text characters are twice as tall as they are wide, so each text character can represent two pixels. This module uses the block characters 9600, 9604, 9608, and the space character to represent pixels.

Since they are still text characters, the "pixels" can be copy/pasted just like any other text. This also makes it easy to display them in terminal windows or IDEs or web apps. The simplicity makes this module flexible for displaying graphics in a terminal window.

This module is intended to be used to teach programming concepts such as nested for loops, simple 2D graphics, cellular automata such as Conway's Game of Life, and other concepts.

The applyFunc() method especially can be used for generating algorithmic art.

The demo folder has example programs that use terminal bitmaps. They include Conway's Game of Life, a Mona Lisa render from a large hexadecimal number, and a rotating cube animation.

This module has no dependencies and fits in a single .py file, so it can be installed through pip or just copied to a tbmp.py file for importing. Copy the singleFileVersion/tbmp.py file to the folder your Python script is in to import tbmp without installing it via pip.

Installation

To install with pip, run:

pip install tbmp

Quickstart Guide

>>> from tbmp import *
>>> b = TBMP(10, 10)  # Create a 10x10 bitmap.
>>> b.width, b.height, b.size
(10, 10, (10, 10))
>>> for i in range(10):
...   b[i, i] = True
...   b[b.width - 1 - i, i] = True
...
>>> print(b)
▀▄      ▄▀
  ▀▄  ▄▀
    ██
  ▄▀  ▀▄
▄▀      ▀▄
>>> print(b.framed)
┌──────────┐
│▀▄      ▄▀│
│  ▀▄  ▄▀  │
│    ██    │
│  ▄▀  ▀▄  │
│▄▀      ▀▄│
└──────────┘
>>> b.invert()
>>> print(b)
▄▀██████▀▄
██▄▀██▀▄██
████  ████
██▀▄██▄▀██
▀▄██████▄▀
>>> b.shift(4, 1)
>>> print(b)
     ▄▄▄▄▄
    █▄▀███
    ███▄▀▀
    ███▀▄▄
    █▀▄███
>>> b.randomize()  # Set pixels randomly
>>> print(b)
▄█▀█▄ ▄█▀▀
 ▀▄█  ▄ ▄▀
▄ █▀█▀▄ ▀▄
▀▄█▀▀  █▀▄
█▀▀██▄▄ ▀
>>> b.randomize(0.90)  # Set 90% of pixels on, at random.
>>> print(b)
█▄█████▄██
▀██▀██████
██▄███████
▄███████▄█
█▄██▄█████
>>> b.randomize(0.25)  # Set 25% of pixels on, at random.
>>> print(b)
▄ ▄▄ █  ▄
▀▄▄▄  ▀
       ▀▄▄
 ▄  ▀▄  ▀
 ▄  ▀  ▀
>>> b.image().save('myBitmap.png')
>>> b.image().show()  # Have Pillow show a preview of the image.

The applyFunc() function is useful for creating algorithmic art. You give it a lambda function (or string of a lambda function) that takes an x and y integer arguments and if the expression is zero, the pixel at those coordinates is black and if nonzero the pixel is white:

>>> b2 = TBMP()  # Create a bitmap slightly smaller than the terminal window.
>>> b2.applyFunc('(x ^ y) % 5')
>>> print(b2)
▄▀██▀▄████▄▀██▀▄████▄▀██▀▄████▄▀██▀▄████▄▀██▀▄████▄▀██▀▄████▄▀██▀▄████▄▀██▀▄███
██▄▀██▀▄▄▀██▀▄████████▄▀██▀▄▄▀██▀▄████████▄▀██▀▄▄▀██▀▄████████▄▀██▀▄▄▀██▀▄█████
▀▄██▄▀████▀▄██▄▀▄▀████████▄▀▀▄████████▀▄▀▄██▄▀████▀▄██▄▀▄▀████████▄▀▀▄████████▀
██▀▄██▄▀▀▄██▄▀████▄▀████▄▀████▀▄████▀▄████▀▄██▄▀▀▄██▄▀████▄▀████▄▀████▀▄████▀▄█
██▄▀██▀▄▄▀██▀▄██▀▄████▄▀████▄▀██▄▀██▀▄████▀▄████████▄▀████▄▀██▀▄██▀▄████▀▄████▄
▄▀██▀▄████▄▀██▀▄██▀▄▄▀████████▄▀██▄▀██▀▄▀▄████████████▄▀▄▀██▀▄██▀▄████████▀▄▄▀█
██▀▄██▄▀▀▄██▄▀████▄▀▀▄██▄▀██████▀▄██▄▀████████▀▄▄▀████████▀▄██▄▀██████▀▄██▄▀▀▄█
▀▄██▄▀████▀▄██▄▀▄▀████▀▄██▄▀██████▀▄██▄▀████▀▄████▄▀████▀▄██▄▀██████▀▄██▄▀████▀
████▄▀██▀▄████▄▀▄▀██▀▄████▄▀██▀▄██▄▀██▀▄████▄▀████▀▄████▄▀██▀▄██▄▀██▀▄████▄▀██▀
██████▄▀██▀▄▄▀████▄▀██▀▄▄▀██▀▄██▄▀██▀▄████████▄▀▀▄████████▄▀██▀▄██▄▀██▀▄▄▀██▀▄█
▄▀████████▄▀▀▄██▀▄██▄▀████▀▄██▄▀██▀▄██▄▀▄▀████████████▀▄▀▄██▄▀██▀▄██▄▀████▀▄██▄
██▄▀████▄▀████▀▄██▀▄██▄▀▀▄██▄▀██▀▄██▄▀████▄▀████████▀▄████▀▄██▄▀██▀▄██▄▀▀▄██▄▀█
▀▄████▄▀████▄▀████▄▀██▀▄▄▀██▀▄██████▄▀████▄▀██▀▄▄▀██▀▄████▀▄██████▄▀██▀▄▄▀██▀▄█
██▀▄▄▀████████▄▀▄▀██▀▄████▄▀██▀▄██████▄▀▄▀██▀▄████▄▀██▀▄▀▄██████▄▀██▀▄████▄▀██▀
██▄▀▀▄██▄▀████████▀▄██▄▀▀▄██▄▀██▄▀████████▀▄██▄▀▀▄██▄▀████████▀▄██▀▄██▄▀▀▄██▄▀█
▄▀████▀▄██▄▀████▀▄██▄▀████▀▄██▄▀██▄▀████▀▄██▄▀████▀▄██▄▀████▀▄██▀▄██▄▀████▀▄██▄
██▀▄████▄▀██▀▄████▄▀██▀▄████▄▀██▄▀██▀▄████▄▀██▀▄████▄▀██▀▄████▄▀████▄▀██▀▄████▄
▀▄████████▄▀██▀▄▄▀██▀▄████████▄▀██▄▀██▀▄▄▀██▀▄████████▄▀██▀▄▄▀████████▄▀██▀▄▄▀█
██████▀▄▀▄██▄▀████▀▄██▄▀▄▀██████▀▄██▄▀████▀▄██▄▀▄▀████████▄▀▀▄██▄▀████████▄▀▀▄█
████▀▄████▀▄██▄▀▀▄██▄▀████▄▀██████▀▄██▄▀▀▄██▄▀████▄▀████▄▀████▀▄██▄▀████▄▀████▀
▄▀██▀▄████▀▄████████▄▀████▄▀██▀▄██▄▀██▀▄▄▀██▀▄██▀▄████▄▀████▄▀██▀▄████▄▀████▄▀█
██▄▀██▀▄▀▄████████████▄▀▄▀██▀▄██▄▀██▀▄████▄▀██▀▄██▀▄▄▀████████▄▀██▀▄▄▀████████▄

The tbpm module also comes with a cube() and monaLisa() function that return premade TBMP objects:

>>> print(cube())
       ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
     ▄▀█           ▄▀█
  ▄▀▀  █        ▄▀▀  ▀▄
▄█▄▄▄▄▄▄█▄▄▄▄▄▄▀      █
█       █     █       █
█       █     ▀▄      █
 █      ▀▄     █      ▀▄
 █       █     █       █
 █      ▄▀▀▀▀▀▀█▀▀▀▀▀▀█▀
 ▀▄  ▄▄▀       ▀▄  ▄▄▀
  █▄▀           █▄▀
  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
>>> print(monaLisa())
▀▄▀ █ ▀▀▄▀ ▀▄▀▀▄▀ ▀▄▀ ▀▄▀▄▀▀▄ ▄▀▄▀ ▄ █ █ ▀▄▀ ▀▄▀ ▀ ▄▀▄▀ ▀ █ ▀▄▀ █ █
▄ █ ▄▀▄ ▀▄▀▄ ▀  ▀▀  ▀▀▄▄ ▄ ▄  ▀▄ ▀▀▄ ▀▄ █ ▄▀▄ ▀ █ ▀▄  █ █ ▄▀▄ █  █▄
▄▄▀ ▄▄▀▄▀ ▄▄▀▀▀▄▀▄▀ █ ▄ ▀▄▄▀ █ ▀▄▀▄ █ ▄ ▄▀ █ ▀▀▄ ▀▄▀▀ ▄ █ ▄▀ ▄▄▀▄▄▄▀
▄▄▀▄▄ ▄ █  █ █▄ █ █ ▄▀▄█ ▄ █ █ █▄ █ █▄▀▄ █ ▄▀▀▄▀█ ▄▀▀▄▀▄ █ █▄ █ ▄ █
 ▀▄▀▄█ █ █▀ █ ▄▀▄▀▄▀▄▀ ▄▀▄▀▄▀ ▀       ▀▄▀▄▀▄▀▄▄ ▀▄▄▀▄▄▀▄▀▄▀▄▀▄▄▀▄▀█
█ █▄ █▄ █ ▀█ █ █ █ █▄▀█ █ ▀▀   ▀▄         ▀▄▀▄▀▀▄ █▄▄ █ █ █ █ █ █ █▄
▄▀ █▀ █▄▀█▀▄▀▄▀▄█▀▄▄▀▄▀█▄▀ ▄▄█▀▄▄ ▀        ▀▄▄█▀▄▀▄▄▀█ █▄▀▄█ █▄▀▀▀▄▄
▀█ ▀█ █ █ ██▄▀█ █▀▄▀██ █  █▄███▄██ ▀▄        ▄█▄▀▄▄█ █▀▄▀█▄ █ █▀█▄█
▀▄█▀█▀▄█▀█▄▄▀█▄▀█▄▀█ █▀  ▀▄█▄▀██▄▀█▄ ▄        ▄▀▀▄ █▄▀▀▄█▀▀█▀▄█▄▀▄█▀
▄▄ ▀█▄ ▄▀▄▀█ ██ ██ ██ ▀  █▄███▄███▄█ █        ▀ ▀▄▄ ▀▀█ ██ ██ █▄▀▄▀▀
  ▀  ▄ ▀▄▀ ▀▄ ▀▀▄▀█▀█▀   ▀  ▀▄▀  ▄ ▀  ▀        ▀▄  ▄▀ ▄▀▄██ ██▄▄▀▄▀▄
  ▀▄  ▀▄  ▀▄  ▀ █▄▀▀▄▀   █ █ ██ ▄█▄ █▄▀         ▄ ▄ ▄ ▄ █▄▀█▄ █ █ █
 ▄ ▄ ▀ ▀ ▀   ▀ ▀  ▄ ▄ ▄  ██▀█▄▄▀▄█▄█▄█ ▀       ▄ ▄ ▄     █▀▄▀█ █ ▀▄▀
 ▄   ▀  ▀  ▀▄ ▄           ██ ██ ▄▀█▄▄              ▀ ▀▀▀ █ ▀ ▀▄ ▀ ▀
    ▄ ▀ ▀▄ ▀    ▀  ▄    ▀ ▀▄█▄  ▀▀█ ▄ ▄ ▄      ▄   ▄   ▀   ▀ ▀  ▀  ▀
 ▄   ▄    ▄ ▄   ▀   ▀     ▀▄▄▀█  █ ▀▄    ▀      ▀   ▄   ▀ ▀▄ ▀  ▀▄
   ▄    ▀     ▄  ▀ ▄ ▄      ▀█▄▀▄▀▀              ▀ ▀  ▄  ▀     ▄  ▄
   ▄    ▀  ▀  ▄   ▄ ▄  ▄      ▀                   ▀ ▄  ▀  ▀ ▀ ▄ ▄ ▄
      ▀  ▀  ▀   ▀▄ ▀▄          ▀▄ ▀ ▄  ▀     ▄   ▄  ▄     ▄  ▄   ▄
 ▄     ▀▄ ▄ ▀ ▀▄ ▀  ▀▄    ▄    █ █  █ █ ▀▄        ▀   ▀ ▀ ▄▀▄ ▀▄  ▀
   ▀ ▄ ▀ ▄  ▄     ▀          ▄▄█▄█▀▄▄█ █▄          ▄  ▀  ▄ █  ▀  ▄ ▀
▄     ▀▄   ▄ ▀     ▀  ▄    ██▄█▄█▀█▄▀██▄▀  ▀ ▀        ▀█▄▀▄▄ ▀▄  ▀▄
  ▀ ▄   ▄▀▄  ▀  ▄        ▄▀█▄█▄█▀██▀██▄█▀▄      ▄    ▀   ▀▄ ▀ ▄▀▄
 ▀   ▀   ▀▄▀ ▀  ▄ ▀    ▄▄▄▀██▀██▀███▄▀█▄▀  ▀     █     ▀ ▄ ▀▀▀▄ ██▄
  ▀ ▄ ▀    ▀ ▀▄▄      ▄▄▀█▀█▀██▀███▀███▄▀▄ ▄▀▀▀▀  ▀          ▀ ▄ ▄▄▀
 ▄   ▀▄ ▀ █▄▄          ▀▀██▀████▄███▄██ █    ▀                    ▀▄
   ▄ ▀ ▀ ▀ ▄       ▄   ▀ ▄ ▀▄▀▀▀█▀█▀█ ▀  ▄     ▄   ▄       ▀ ▀ ▀ ▄
▀   █  ▀▄  █ ▀      ▀▄     ▄ ▀ ▄  ▄  ▄               ▀        ▄  ▀
      ▀  ▀    ▀          ▄   ▄  ▀  ▄        ▄  ▄ ▄         ▄     ▄
▀   ▀▄ ▀▄▄       ▄        ▀                      ▄  ▀▄       ▀ ▄
  ▀▄▀▄▀▄ ▀▀    ▀              ▀  ▀     ▀       ▀                 ▀
▀    ▄  ▀  ▄       ▀   ▄  ▄      ▄                         ▀   ▀  ▀
▄  ▀   ▄      ▄     ▄        ▀             ▀                    ▄
   ▄           ▀             ▄     ▄                  ▀  ▄       ▄
▄         ▀                          ▄      ▀    ▀
 ▀    ▀    ▄ ▄   ▄   ▀   ▀    ▀                              ▀    ▀
▄       ▀  ▄   ▀    ▄ ▄                         ▄       ▄
 ▀  ▄   ▀▄  ▀  ▀ ▄▄▄▀██▀█▄▀▄ ▄        ▀           ▀       ▄
             ▀    ▄▀█ █▀▄█▄█▀▄▀▄                               ▀
              ▀     ▀▄ ▀ █ ▀█ █▀██▄     ▄         ▄      ▄
 ▄                ▄ ▄ ▀▄▀▄▀▀▄▀▄▄ ▀ ▀▄▀ ▀  ▀ ▀ ▀ ▀             ▀
     ▄          ▄ ▄ ▀█   ▀ █▄  ▀█▄   ▀ ▀▄   ▄      ▄
   ▀        ▀   ▄ ▄▀ █▀▀ ▀   ▀▄▄  ▀▄     ▄      ▄             ▀   ▀
        ▀         ▀▄ ▀▀▀  ▀    ▀    ▀▄                 ▀
                 ▀ ▄▀    ▄  ▄   ▄
                     ▄                                       ▀
            ▄
                                        ▀
    ▀           ▀             ▀                                ▀
                        ▀

Contribute

If you'd like to contribute to tbmp, check out https://github.com/asweigart/tbmp

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tbmp-0.1.2.tar.gz (26.8 kB view details)

Uploaded Source

File details

Details for the file tbmp-0.1.2.tar.gz.

File metadata

  • Download URL: tbmp-0.1.2.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for tbmp-0.1.2.tar.gz
Algorithm Hash digest
SHA256 165923d07166949ef322f97ecb4f612f7e170cf8977dc33ba30a27d37033606b
MD5 00f632ab06675bc7b0af91fe219effe9
BLAKE2b-256 cc4309327181ef01a9fe73324fe76fa28074dc7345332df05764b9549a3ee8fd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page