Skip to main content

Color image buffer

What this example shows​

Draw a small image straight from a buffer in your code, with no file to decode. The buffer holds two pixels per byte: the high four bits are the first pixel's color index, the low four bits the second's. The example builds a 180 × 90 pixel image of six vertical stripes, one per color.

Example code​

"""Draw a small pre-packed color image buffer directly onto the screen.

draw_color_image() takes a raw 4bpp buffer (2 pixels per byte, row-major, high
nibble first) instead of decoding a file -- useful for small pre-rendered
icons/patterns baked directly into your code instead of loaded from SD/web.
"""

from inkplate7spectra import Inkplate

inkplate = Inkplate()
inkplate.begin()

# Build a small pixel image cycling through all 6 available colors in vertical
# stripes, two pixels packed per byte (high nibble = first pixel, low nibble =
# second). Sized 180x90 so the stripes are clearly visible on screen.
IMG_WIDTH = 180
IMG_HEIGHT = 90
colors = [
Inkplate.BLACK,
Inkplate.WHITE,
Inkplate.YELLOW,
Inkplate.RED,
Inkplate.BLUE,
Inkplate.GREEN,
]

stripe_width = IMG_WIDTH // len(colors)
bytes_per_row = IMG_WIDTH // 2
image = bytearray(bytes_per_row * IMG_HEIGHT)
for y in range(IMG_HEIGHT):
for xb in range(bytes_per_row):
c1 = colors[min((2 * xb) // stripe_width, len(colors) - 1)]
c2 = colors[min((2 * xb + 1) // stripe_width, len(colors) - 1)]
image[y * bytes_per_row + xb] = (c1 << 4) | c2

# Draw it near the top-left corner
inkplate.draw_color_image(50, 50, IMG_WIDTH, IMG_HEIGHT, image)

inkplate.display()

Functions used​

Inkplate()returns Inkplate

Creates the display object from the inkplate7spectra module.

ReturnsThe display object.

inkplate.begin()returns bool

Sets up the board and a white frame buffer. Call it once.

ReturnsTrue.

inkplate.draw_color_image(x, y, width, height, image)returns None

Draws a packed buffer, two pixels per byte with the high four bits first, row by row, with its top-left corner at (x, y). Each four-bit value is a color index, 0 to 5.

ReturnsNothing.

inkplate.display(leave_on=False)returns None

Sends the frame buffer to the panel and refreshes it.

ReturnsNothing.

What to expect​

A 180 × 90 pixel block appears 50 pixels from the top-left corner, made of six stripes 30 pixels wide: black, white, yellow, red, blue and green. Nothing is printed in the shell.

🔗Open the complete example

examples/inkplate7spectra/draw_color_image.py

→