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 bitmap.

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

  • 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)[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) – The circle color.

draw_image(bitmap, x=0, y=0, start_x=0, start_y=0, end_x=None, end_y=None)[source]

Copy one bitmap onto another.

Parameters
  • bitmap (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

  • 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. Note: JPG file format support is limited to * Sampling: 4:4:4 (recommended), 4:2:0, or 4:2:2 * Cb/Cr sampling factor: 1 * Y/Cb/Cr or Grayscale

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)[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) – The line color.

draw_rect(x1, y1, width, height, color)[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) – Color of the rectangle.

draw_text(text, x=0, y=0, color=(255, 255, 255), scale=1, background=None)[source]

Draw text or an image onto this Bitmap.

This doesn’t do any scrolling or wrap-around if text won’t fit.

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

  • 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.

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

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

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

fill(color)[source]

Fill the Bitmap with the given color.

Parameters

color (tuple) – The fill color.

fill_circle(x, y, radius, color)[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) – The circle color.

fill_rect(x1, y1, width, height, color)[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) – Color of the rectangle.

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 bitmap. 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 bitmap.

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 or set the transparent color value.

The default value is None for no-transparency.

property width

Get the width of the bitmap in pixels.

Download Source