This example shows how to load and display a bitmap (.bmp) file. We will still need to create all the pieces - a TileGrid, a Group, etc. But they can be used in their most simple way.
BMP File Format
Not all BMP file formats are supported. You will need to make sure you have an indexed BMP file. Follow the link below for some good info on how to convert or create such a BMP file:
OnDiskBitmap
First, let's use OnDiskBitmap to source the bitmap image directly from flash memory storage. This is like reading the image from disk instead of loading it into memory first (we'll do that next). The trade off here is the reduced use of memory for potentially slower pixel draw times.
We'll use a 320x240 pixel image. Here's the image:
Here's the code:
# SPDX-FileCopyrightText: 2019 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import board
import displayio
display = board.DISPLAY
# Setup the file as the bitmap data source
bitmap = displayio.OnDiskBitmap("/purple.bmp")
# Create a TileGrid to hold the bitmap
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)
# Create a Group to hold the TileGrid
group = displayio.Group()
# Add the TileGrid to the Group
group.append(tile_grid)
# Add the Group to the Display
display.root_group = group
# Loop forever so you can enjoy your image
while True:
pass
ImageLoad
This approach use the CircuitPython Image Load library to load the image into memory and then display it. Using the same image from above, here's the code:
# SPDX-FileCopyrightText: 2019 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import board
import displayio
import adafruit_imageload
display = board.DISPLAY
bitmap, palette = adafruit_imageload.load("/purple.bmp",
bitmap=displayio.Bitmap,
palette=displayio.Palette)
# Create a TileGrid to hold the bitmap
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
# Create a Group to hold the TileGrid
group = displayio.Group()
# Add the TileGrid to the Group
group.append(tile_grid)
# Add the Group to the Display
display.root_group = group
# Loop forever so you can enjoy your image
while True:
pass
Page last edited January 22, 2025
Text editor powered by tinymce.