PixelArray
pykraken.PixelArray Represents a 2D pixel buffer for image manipulation and blitting operations.
More about PixelArray
A PixelArray is a 2D array of pixels that can be manipulated, drawn on, and used as a source for texture creation or blitting to other PixelArrays. Supports pixel-level operations, color key transparency, and alpha blending.
At a glance
13 membersReference
PYTHON APIPixelArray(width: int, height: int)PixelArray(file_path: str | PathLike)Overloaded function.
PixelArray(width: int, height: int)
Create a new PixelArray with the specified dimensions.
Args
- width (
int) — The width of the pixel array. - height (
int) — The height of the pixel array.
Raises
- RuntimeError — If pixel array creation fails.
PixelArray(file_path: str | PathLike)
Create a PixelArray by loading an image from a file.
Args
- file_path (
str | PathLike[str]) — Path to the image file to load.
Raises
- RuntimeError — If the file cannot be loaded or doesn't exist.
alpha_mod
propertyalpha_mod: intThe alpha modulation value for the pixel array.
Controls the overall transparency of the pixel array. Values range from 0 (fully transparent) to 255 (fully opaque).
Returns
- int — The current alpha modulation value [0-255].
Raises
- RuntimeError — If getting the alpha value fails.
color_key
propertycolor_key: ColorThe color key for transparency.
When set, pixels of this color will be treated as transparent during blitting operations. Used for simple transparency effects.
Returns
- Color — The current color key.
Raises
- RuntimeError — If getting the color key fails.
height
propertyheight: intThe height of the pixel array.
Returns
- int — The pixel array height.
size
propertysize: Vec2The size of the pixel array as a Vec2.
Returns
- Vec2 — The pixel array size as (width, height).
width
propertywidth: intThe width of the pixel array.
Returns
- int — The pixel array width.
blit(self, pixel_array: PixelArray, pos: Vec2, anchor: Vec2 = Vec2(0.000000, 0.000000), src: Rect = Rect(x=0.000000, y=0.000000, w=0.000000, h=0.000000)) -> Noneblit(self, pixel_array: PixelArray, dst: Rect, src: Rect = Rect(x=0.000000, y=0.000000, w=0.000000, h=0.000000)) -> NoneOverloaded function.
blit(self, pixel_array: PixelArray, pos: Vec2, anchor: Vec2 = Vec2(0.000000, 0.000000), src: Rect = Rect(x=0.000000, y=0.000000, w=0.000000, h=0.000000)) -> None
Blit (copy) another pixel array onto this pixel array at the specified position with anchor alignment.
Args
- pixel_array (
PixelArray) — The source pixel array to blit from. - pos (
Vec2) — The position to blit to. - anchor (
Vec2, optional) — The anchor point for positioning. Defaults to (0,0) TopLeft. - src (
Rect, optional) — The source rectangle to blit from. Defaults to entire source pixel array.
Raises
- RuntimeError — If the blit operation fails.
blit(self, pixel_array: PixelArray, dst: Rect, src: Rect = Rect(x=0.000000, y=0.000000, w=0.000000, h=0.000000)) -> None
Blit (copy) another pixel array onto this pixel array with specified destination and source rectangles.
Args
- pixel_array (
PixelArray) — The source pixel array to blit from. - dst (
Rect) — The destination rectangle on this pixel array. - src (
Rect, optional) — The source rectangle to blit from. Defaults to entire source pixel array.
Raises
- RuntimeError — If the blit operation fails.
copy
methodcopy(self) -> PixelArrayCreate a copy of this pixel array.
Returns
- PixelArray — A new PixelArray that is an exact copy of this one.
Raises
- RuntimeError — If pixel array copying fails.
fill
methodfill(self, color: Color) -> NoneFill the entire pixel array with a solid color.
Args
- color (
Color) — The color to fill the pixel array with.
get_at
methodget_at(self, x: int, y: int) -> ColorGet the color of a pixel at the specified coordinates.
Args
- x (
int) — The x-coordinate of the pixel. - y (
int) — The y-coordinate of the pixel.
Returns
- Color — The color of the pixel at the specified coordinates.
Raises
- IndexError — If coordinates are outside the pixel array bounds.
get_rect
methodget_rect(self) -> RectGet a rectangle representing the pixel array bounds.
Returns
- Rect — A rectangle with position (0, 0) and the pixel array's dimensions.
scroll
methodscroll(self, dx: int, dy: int, scroll_mode: ScrollMode) -> NoneScroll the pixel array's contents by the specified offset.
Args
- dx (
int) — Horizontal scroll offset in pixels. - dy (
int) — Vertical scroll offset in pixels. - scroll_mode (
ScrollMode, optional) — Behavior for pixels scrolled off the edge.- REPEAT: Wrap pixels around to the opposite edge.
- ERASE: Fill scrolled areas with transparent pixels.
- SMEAR: Extend edge pixels into scrolled areas.
set_at
methodset_at(self, x: int, y: int, color: Color) -> NoneSet the color of a pixel at the specified coordinates.
Args
- x (
int) — The x-coordinate of the pixel. - y (
int) — The y-coordinate of the pixel. - color (
Color) — The color to set the pixel to.
Raises
- IndexError — If coordinates are outside the pixel array bounds.