bitmap – Core Graphics Rendering

class bitmap.Bitmap(width: int, height: int, is_full_color: bool = False)[source]

Storage for a 2D bitmap.

The alternative to this abstraction is to keep raster data in a python list. This will be much faster to render by the C code.

This class contains all the pixel-access functions (lines, fill, text, individuals, etc).

The Canvas class extends this class. A Canvas IS-A bitmap with additional features (namely position, child Canvases, color palette, visibility, scale, and text cursor).

get_dimensions()[source]

Returns the dimension (width, height) of the Bitmap.

Returns

Width and height of the Bitmap.

Return type

(int, int)

set_pixel(x: int, y: int, color: int) → None[source]

Set a single pixel value in the raster.

Parameters
  • x (int) – The x coordinate,

  • y (int) – The y coordinate,

  • color (int) – The color value.

get_pixel(x: int, y: int) → int[source]

Get the color of a single pixel from the raster.

Parameters
  • x (int) – The x coordinate.

  • y (int) – The y coordinate.

Returns

The color value.

Return type

int

clear() → None[source]

Clear the Bitmap to BLACK.

fill(color: int) → None[source]

Fill the Bitmap with the given color.

Parameters

color (int) – The fill color.

rect(x: int, y: int, width: int, height: int, color: int) → None[source]

Draw a rectangle on the bitmap.

Parameters
  • x (int) – Upper left corner of rectangle.

  • y (int) – Upper left corner of rectangle.

  • width (int) – Width of rectangle.

  • height (int) – Height of rectangle.

  • color (int) – Color of the rectangle.

fill_rect(x: int, y: int, width: int, height: int, color: int) → None[source]

Draw a filled rectangle on the bitmap.

Parameters
  • x (int) – Upper left corner of rectangle.

  • y (int) – Upper left corner of rectangle.

  • width (int) – Width of rectangle.

  • height (int) – Height of rectangle.

  • color (int) – Color of the rectangle.

circle(x: int, y: int, radius: int, color: int = 1) → None[source]

Draw a circle on the Bitmap.

Parameters
  • x (int) – The starting x coordinate.

  • y (int) – The starting y coordinate.

  • radius (int) – The circle radius.

  • color (int, optional) – The circle color. Default is (WHITE).

line(x1: int, y1: int, x2: int, y2: int, color: int = 1) → None[source]

Draw a line on the Bitmap.

Parameters
  • x1 (int) – The starting x coordinate.

  • y1 (int) – The starting y coordinate.

  • x2 (int) – The ending x coordinate.

  • y2 (int) – The ending y coordinate.

  • color (int, optional) – The line color. Default is (WHITE).

draw_image(raster: bitmap.Bitmap, x: int = 0, y: int = 0, start_x: int = 0, start_y: int = 0, width: int = None, height: int = None) → None[source]

Copy one bitmap onto another.

Parameters
  • raster (Bitmap) – The bitmap to copy from.

  • x (int) – The destination x coordinate.

  • y (int) – The destination y coordinate.

  • start_x (int, optional) – The starting window on the read-from bitmap. Default is 0.

  • start_y (int, optional) – The starting window on the read-from bitmap. Default is 0.

  • width (int, optional) – The window width of the read-from bitmap. Default is full-width.

  • height (int, optional) – The window height of the read-from bitmap. Default is full-height.

draw_text(text: str, scale: int = 1, color: int = 1, background: int = None, x: int = 0, y: int = 0) → None[source]

Draw text or an image onto this Bitmap.

This uses a built-in 5x7 font. We don’t do any scrolling. All we do here is twiddle pixels for the font.

Parameters
  • text (object) – The text to display.

  • scale (int, optional) – The scaling factor. Default is 1.

  • color (int, optional) – The text color. Default is (WHITE).

  • background (int, optional) – The background color. Default is None (Transparent).

  • x (int, optional) – The x coordinate of upper left corner of text. Default is 0.

  • y (int, optional) – The y coordinate of upper left corner of text. Default is 0.

Download Source