bitmap – Pixel manipulations

class bitmap.Bitmap(width, height, transparent_color=(247, 251, 247))[source]

A rectangular area of colored pixels.

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

Bitmaps are a 2D list of pixels. Each pixel is a tuple of 3 values: red, blue, green in that order.

Create a new bitmap.

Parameters
  • width (int) – Pixel width of the raster.

  • height (int) – Pixel height of the raster.

  • transparent_color (bool, optional) – The pixel value considered to be transparent (default is colors.TRANSPARENT).

clear()[source]

Clear the Bitmap to BLACK or transparent if set.

draw_circle(x, y, radius, color=(255, 255, 255))[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 (tuple, optional) – The circle color. Default is WHITE.

draw_image(raster, x=0, y=0, start_x=0, start_y=0, end_x=None, end_y=None, scale=1)[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.

  • end_x (int, optional) – The ending corner on the read-from bitmap. Default is width.

  • end_y (int, optional) – The ending corner on the read-from bitmap. Default is height.

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

draw_jpg(filename, start_x, start_y, x, y, width, height)[source]

Draw a JPG file into the bitmap.

Parameters
  • filename (str) – name of the JPG file.

  • start_x (int, optional) – starting corner in the JPG image. Default is 0.

  • start_y (int, optional) – starting corner in the JPG image. Default is 0.

  • x (int, optional) – starting corner on the bitmap. Default is 0.

  • y (int, optional) – starting corner on the bitmap. Default is 0.

  • width (int, optional) – width of the JPG area to use. Default is None for all.

  • height (int, optional) – height of the JPG area to use. Default is None for all.

draw_line(x1, y1, x2, y2, color=(255, 255, 255))[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 (tuple, optional) – The line color. Default is WHITE.

draw_rect(x1, y1, width, height, color=(255, 255, 255))[source]

Draw a rectangle on the bitmap.

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

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

  • width (int) – Width of rectangle.

  • height (int) – Height of rectangle.

  • color (tuple, optional) – Color of the rectangle. Default is WHITE.

draw_text(text, scale=1, color=(255, 255, 255), background=None, x=0, y=0)[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 (tuple, 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.

fill(color)[source]

Fill the Bitmap with the given color.

Parameters

color (tuple) – The fill color.

fill_circle(x, y, radius, color=(255, 255, 255))[source]

Draw a filled circle on the Bitmap.

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

  • y (int) – The starting y coordinate.

  • radius (int) – The circle radius.

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

fill_rect(x1, y1, width, height, color=(255, 255, 255))[source]

Draw a filled rectangle on the bitmap.

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

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

  • width (int) – Width of rectangle.

  • height (int) – Height of rectangle.

  • color (tuple, optional) – Color of the rectangle. Default is WHITE.

static from_arr(data)[source]

Create a Bitmap from a 2D array of integers.

Parameters

data (list) – 2D list of integers (default color mapping).

static from_ascii(ascii_img, transparent_color, scale, color_map)[source]

Create a Bitmap from a list of ASCII Art row-strings and a color map.

Note: This function doesn’t do error-checking, as it expects to be called with a properly-formed input generated by ascii_art.py.

Parameters
  • ascii_img (list) – list of strings “….###..$$…” to be translated into pixels.

  • transparent_color (tuple) – The pixel value considered to be transparent.

  • scale (int) – The scaling factor.

  • color_map – dictionary mapping string chars to color tuples.

get_pixel(x, y)[source]

Get the color of a single pixel from the raster. Note that this may be different than the color tuple set by set_pixel() since colors are stored in 16-bit format natively. To compare the color returned by this function with a 24-bit RGB color, use the color16() function.

Example:

set_pixel(35, 35, WHITE)
my_color = get_pixel(35, 35)
if my_color == color16(WHITE):
    # Found my color!
Parameters
  • x (int) – The x coordinate.

  • y (int) – The y coordinate.

Returns

The color value.

Return type

tuple

property height

Get the height of the bitmap in pixels.

set_pixel(x, y, color)[source]

Set a single pixel value in the raster.

Parameters
  • x (int) – The x coordinate,

  • y (int) – The y coordinate,

  • color (tuple) – The color value. Accepts either 24-bit or 16-bit RGB. Will truncate to 16-bit.

property transparent_color

Get the transparent color value.

The default value is None for no-transparency.

property width

Get the width of the bitmap in pixels.

Download Source