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
-
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_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_rect
(x1, y1, width, height, color=(255, 255, 255))[source]¶ Draw a filled rectangle on the bitmap.
-
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.
-
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 thecolor16()
function.Example:
set_pixel(35, 35, WHITE) my_color = get_pixel(35, 35) if my_color == color16(WHITE): # Found my color!
-
property
height
¶ Get the height of the bitmap in pixels.
-
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.