bitmap
– Pixel manipulations¶
-
class
codex.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
(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_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.
-
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 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 thecolors.color16()
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 or set the transparent color value.
The default value is None for no-transparency.
-
property
width
¶ Get the width of the bitmap in pixels.