Skip to content

framebuf

pygraphics.framebuf

framebuf.py - a pure-Python, drop-in replacement for MicroPython's built-in framebuf module (https://docs.micropython.org/en/latest/library/framebuf.html) for use on CPython and CircuitPython.

This module intentionally mirrors MicroPython's C implementation (extmod/modframebuf.c, by Damien P. George) as closely as possible:

  • Same format constants, same method signatures, same return values (None for every drawing method; only pixel() without a color argument returns a value).
  • No pydisplay extensions (no RGB888, no Area return values, no arc/circle/blit_rect/etc.) and no imports from pygraphics.

For the pydisplay-flavored subclass that adds Area bounding boxes, RGB888, extra shapes, and file I/O, see pygraphics.FrameBuffer (graphics/_framebuf_plus.py), which subclasses this module.

FrameBuffer(buffer, width, height, format, stride=None)

FrameBuffer object.

Parameters:

Name Type Description Default
buffer bytearray

An object with a buffer protocol, large enough for every pixel defined by width, height and format.

required
width int

The width of the frame buffer in pixels.

required
height int

The height of the frame buffer in pixels.

required
format int

The format of the frame buffer. One of: - MONO_VLSB: Single bit displays (like SSD1306 OLED) - MONO_HLSB: Single bit files like PBM (Portable BitMap) - MONO_HMSB: Single bit displays where the bits in a byte are horizontally mapped Each byte occupies 8 horizontal pixels with bit 0 being the leftmost. - RGB565: 16-bit color displays - GS2_HMSB: 2-bit color displays like the HT16K33 8x8 Matrix - GS4_HMSB: 4-bit grayscale displays - GS8: 8-bit grayscale/palette displays

required
stride int

The number of pixels between each horizontal line of the frame buffer If not given, it is assumed to be equal to the width.

None

height property

The height of the FrameBuffer in pixels.

width property

The width of the FrameBuffer in pixels.

blit(source, x, y, key=-1, palette=None)

Draw another FrameBuffer (or readonly tuple/list) on top of this one at (x, y).

ellipse(x, y, xr, yr, c, f=False, m=_ELLIPSE_MASK_ALL)

Draw an ellipse centered at (x, y) with radii xr, yr. f fills it.

m is a 4-bit quadrant mask (bit 0 = Q1 top-right, bit 1 = Q2 top-left, bit 2 = Q3 bottom-left, bit 3 = Q4 bottom-right).

fill(c)

Fill the entire FrameBuffer with the specified color.

Equivalent to fill_rect(0, 0, width, height, c) (matching MicroPython, which has no separate per-format fill).

fill_rect(x, y, w, h, c)

Draw a filled rectangle at the given location and size, in the given color.

hline(x, y, w, c)

Draw a single pixel wide horizontal line.

line(x1, y1, x2, y2, c)

Draw a single pixel wide line from (x1, y1) to (x2, y2).

pixel(x, y, c=None)

Get or set the color of a given pixel.

If c is not given, the color of the pixel is returned (or None if x, y is out of bounds). If c is given, the pixel is set and None is returned (a no-op if x, y is out of bounds).

poly(x, y, coords, c, f=False)

Draw a closed polygon at (x, y) using an array/list of coordinate pairs.

coords may be an array('h', [x0, y0, x1, y1, ...]) (as documented for MicroPython) or a flat list/tuple of ints, or a list of (x, y) pairs. f fills the polygon; otherwise only the outline is drawn.

rect(x, y, w, h, c, f=False)

Draw a rectangle at the given location, size and color. f fills it.

scroll(xstep, ystep)

Shift the contents of the FrameBuffer by (xstep, ystep) pixels.

text(s, x, y, c=1)

Write text to the FrameBuffer using (x, y) as the upper-left corner.

All characters are 8x8 pixels; there is no way to change the font.

vline(x, y, h, c)

Draw a single pixel wide vertical line.

GS2HMSBFormat

2-bit grayscale, horizontally mapped MSB-first (e.g. HT16K33 8x8 Matrix).

GS4HMSBFormat

4-bit grayscale, horizontally mapped, two pixels per byte.

GS8Format

8-bit grayscale/palette, one byte per pixel.

MHLSBFormat

Single bit displays, horizontally mapped, bit 0 = rightmost pixel (e.g. PBM).

MHMSBFormat

Single bit displays, horizontally mapped, bit 0 = leftmost pixel.

MVLSBFormat

Single bit displays, vertically mapped (e.g. SSD1306 OLED).

RGB565Format

16-bit color, two bytes per pixel, little-endian.

FrameBuffer1(buffer, width, height, format, stride=None)

Create a new FrameBuffer object. Here only for historical reasons.