pygame module for image masks.
Useful for fast pixel perfect collision detection. A mask uses 1 bit per-pixel to store which parts collide.
New in pygame 1.8.
pygame.mask.from_surface(Surface) -> Maskpygame.mask.from_surface(Surface, threshold=127) -> MaskCreates a Mask from the given surface
Creates a Mask object from the given surface by setting all the opaque pixels and not setting the transparent pixels.
If the surface uses a color-key, then it is used to decide which bits in the resulting mask are set. All the pixels that are not equal to the color-key are set and the pixels equal to the color-key are not set.
If a color-key is not used, then the alpha value of each pixel is used to decide which bits in the resulting mask are set. All the pixels that have an alpha value greater than the threshold parameter are set and the pixels with an alpha value less than or equal to the threshold are not set.
| Parameters: |
|
|---|---|
| Returns: |
a newly created |
| Return type: |
pygame.mask.from_threshold(Surface, color) -> Maskpygame.mask.from_threshold(Surface, color, threshold=(0, 0, 0, 255), othersurface=None, palette_colors=1) -> MaskCreates a mask by thresholding Surfaces
This is a more featureful method of getting a Mask from a surface.
If the optional othersurface is not used, all the pixels within the threshold of the color parameter are set in the resulting mask.
If the optional othersurface is used, every pixel in the first surface that is within the threshold of the corresponding pixel in othersurface is set in the resulting mask.
| Parameters: |
|
|---|---|
| Returns: |
a newly created |
| Return type: |
pygame object for representing 2D bitmasksMask(size=(width, height)) -> MaskMask(size=(width, height), fill=False) -> Mask
A Mask object is used to represent a 2D bitmask. Each bit in the mask represents a pixel. 1 is used to indicate a set bit and 0 is used to indicate an unset bit. Set bits in a mask can be used to detect collisions with other masks and their set bits.
A filled mask has all of its bits set to 1, conversely an unfilled/cleared/empty mask has all of its bits set to 0. Masks can be created unfilled (default) or filled by using the fill parameter. Masks can also be cleared or filled using the pygame.mask.Mask.clear() and pygame.mask.Mask.fill() methods respectively.
A mask's coordinates start in the top left corner at (0, 0) just like pygame.Surface. Individual bits can be accessed using the pygame.mask.Mask.get_at() and pygame.mask.Mask.set_at() methods.
The methods overlap(), overlap_area(), overlap_mask(), draw(), erase(), and convolve() use an offset parameter to indicate the offset of another mask's top left corner from the calling mask's top left corner. The calling mask's top left corner is considered to be the origin (0, 0). Offsets are a tuple or list of 2 integer values (x_offset, y_offset). Positive and negative offset values are supported.
0 to x (x_offset)
: :
0 ..... +----:---------+
to | : |
y .......... +-----------+
(y_offset) | | othermask |
| +-----------+
| calling_mask |
+--------------+
| Parameters: |
|
|---|---|
| Returns: |
a newly created |
| Return type: |
Changed in pygame 2.0.0: Shallow copy support added. The Mask class supports the special method __copy__() and shallow copying via copy.copy(mask).
Changed in pygame 2.0.0: Subclassing support added. The Mask class can be used as a base class.
Changed in pygame 1.9.5: Added support for keyword arguments.
Changed in pygame 1.9.5: Added the optional keyword parameter fill.
Changed in pygame 1.9.5: Added support for masks with a width and/or a height of 0.
copy() -> MaskReturns a new copy of the mask
| Returns: | a new copy of this mask, the new mask will have the same width, height, and set/unset bits as the original |
|---|---|
| Return type: | Mask |
Note
If a mask subclass needs to copy any instance specific attributes then it should override the __copy__() method. The overridden __copy__() method needs to call super().__copy__() and then copy the required data as in the following example code.
class SubMask(pygame.mask.Mask):
def __copy__(self):
new_mask = super().__copy__()
# Do any SubMask attribute copying here.
return new_mask
New in pygame 2.0.0.
get_size() -> (width, height)Returns the size of the mask
| Returns: | the size of the mask, (width, height) |
|---|---|
| Return type: | tuple(int, int) |
get_rect(**kwargs) -> RectReturns a Rect based on the size of the mask
Returns a new pygame.Rect() object based on the size of this mask. The rect's default position will be (0, 0) and its default width and height will be the same as this mask's. The rect's attributes can be altered via pygame.Rect() attribute keyword arguments/values passed into this method. As an example, a_mask.get_rect(center=(10, 5)) would create a pygame.Rect() based on the mask's size centered at the given position.
| Parameters: |
kwargs (dict) -- pygame.Rect() attribute keyword arguments/values that will be applied to the rect |
|---|---|
| Returns: | a new pygame.Rect() object based on the size of this mask with any pygame.Rect() attribute keyword arguments/values applied to it |
| Return type: | Rect |
New in pygame 2.0.0.
get_at((x, y)) -> intGets the bit at the given position
| Parameters: | pos (tuple(int, int) or list[int, int]) -- the position of the bit to get |
|---|---|
| Returns: | 1 if the bit is set, 0 if the bit is not set |
| Return type: | int |
| Raises: | IndexError -- if the position is outside of the mask's bounds |
set_at((x, y)) -> Noneset_at((x, y), value=1) -> NoneSets the bit at the given position
| Parameters: |
|
|---|---|
| Returns: |
|
| Return type: |
NoneType |
| Raises: |
IndexError -- if the position is outside of the mask's bounds |
overlap(othermask, offset) -> (x, y)overlap(othermask, offset) -> NoneReturns the point of intersection
Returns the first point of intersection encountered between this mask and othermask. A point of intersection is 2 overlapping set bits.
The current algorithm searches the overlapping area in sizeof(unsigned long int) * CHAR_BIT bit wide column blocks (the value of sizeof(unsigned long int) * CHAR_BIT is platform dependent, for clarity it will be referred to as W). Starting at the top left corner it checks bits 0 to W - 1 of the first row ((0, 0) to (W - 1, 0)) then continues to the next row ((0, 1) to (W - 1, 1)). Once this entire column block is checked, it continues to the next one (W to 2 * W - 1). This is repeated until it finds a point of intersection or the entire overlapping area is checked.
| Parameters: |
|
|---|---|
| Returns: |
point of intersection or |
| Return type: |
tuple(int, int) or NoneType |
overlap_area(othermask, offset) -> numbitsReturns the number of overlapping set bits
Returns the number of overlapping set bits between between this mask and othermask.
This can be useful for collision detection. An approximate collision normal can be found by calculating the gradient of the overlapping area through the finite difference.
dx = mask.overlap_area(othermask, (x + 1, y)) - mask.overlap_area(othermask, (x - 1, y)) dy = mask.overlap_area(othermask, (x, y + 1)) - mask.overlap_area(othermask, (x, y - 1))
| Parameters: |
|
|---|---|
| Returns: |
the number of overlapping set bits |
| Return type: |
int |
overlap_mask(othermask, offset) -> MaskReturns a mask of the overlapping set bits
Returns a Mask, the same size as this mask, containing the overlapping set bits between this mask and othermask.
| Parameters: |
|
|---|---|
| Returns: |
a newly created |
| Return type: |
fill() -> NoneSets all bits to 1
Sets all bits in the mask to 1.
| Returns: | None |
|---|---|
| Return type: | NoneType |
clear() -> NoneSets all bits to 0
Sets all bits in the mask to 0.
| Returns: | None |
|---|---|
| Return type: | NoneType |
invert() -> NoneFlips all the bits
Flips all of the bits in the mask. All the set bits are cleared to 0 and all the unset bits are set to 1.
| Returns: | None |
|---|---|
| Return type: | NoneType |
scale((width, height)) -> MaskResizes a mask
Creates a new Mask of the requested size with its bits scaled from this mask.
| Parameters: | size (tuple(int, int) or list[int, int]) -- the width and height (size) of the mask to create |
|---|---|
| Returns: | a new Mask object with its bits scaled from this mask |
| Return type: | Mask |
| Raises: |
ValueError -- if width < 0 or height < 0
|
draw(othermask, offset) -> NoneDraws a mask onto another
Performs a bitwise OR, drawing othermask onto this mask.
| Parameters: |
|
|---|---|
| Returns: |
|
| Return type: |
NoneType |
erase(othermask, offset) -> NoneErases a mask from another
Erases (clears) all bits set in othermask from this mask.
| Parameters: |
|
|---|---|
| Returns: |
|
| Return type: |
NoneType |
count() -> bitsReturns the number of set bits
| Returns: | the number of set bits in the mask |
|---|---|
| Return type: | int |
centroid() -> (x, y)Returns the centroid of the set bits
Finds the centroid (the center mass of the set bits) for this mask.
| Returns: | a coordinate tuple indicating the centroid of the mask, it will return (0, 0) if the mask has no bits set |
|---|---|
| Return type: | tuple(int, int) |
angle() -> thetaReturns the orientation of the set bits
Finds the approximate orientation (from -90 to 90 degrees) of the set bits in the mask. This works best if performed on a mask with only one connected component.
| Returns: | the orientation of the set bits in the mask, it will return 0.0 if the mask has no bits set |
|---|---|
| Return type: | float |
Note
See connected_component() for details on how a connected component is calculated.
outline() -> [(x, y), ...]outline(every=1) -> [(x, y), ...]Returns a list of points outlining an object
Returns a list of points of the outline of the first connected component encountered in the mask. To find a connected component, the mask is searched per row (left to right) starting in the top left corner.
The every optional parameter skips set bits in the outline. For example, setting it to 10 would return a list of every 10th set bit in the outline.
| Parameters: | every (int) -- (optional) indicates the number of bits to skip over in the outline (default is 1) |
|---|---|
| Returns: | a list of points outlining the first connected component encountered, an empty list is returned if the mask has no bits set |
| Return type: | list[tuple(int, int)] |
Note
See connected_component() for details on how a connected component is calculated.
convolve(othermask) -> Maskconvolve(othermask, outputmask=None, offset=(0, 0)) -> MaskReturns the convolution of this mask with another mask
Convolve this mask with the given othermask.
| Parameters: | |
|---|---|
| Returns: |
a If an |
| Return type: |
connected_component() -> Maskconnected_component((x, y)) -> MaskReturns a mask containing a connected component
A connected component is a group (1 or more) of connected set bits (orthogonally and diagonally). The SAUF algorithm, which checks 8 point connectivity, is used to find a connected component in the mask.
By default this method will return a Mask containing the largest connected component in the mask. Optionally, a bit coordinate can be specified and the connected component containing it will be returned. If the bit at the given location is not set, the returned Mask will be empty (no bits set).
| Parameters: | pos (tuple(int, int) or list[int, int]) -- (optional) selects the connected component that contains the bit at this position |
|---|---|
| Returns: | a Mask object (same size as this mask) with the largest connected component from this mask, if this mask has no bits set then an empty mask will be returnedIf the |
| Return type: | Mask |
| Raises: |
IndexError -- if the optional pos parameter is outside of the mask's bounds |
connected_components() -> [Mask, ...]connected_components(min=0) -> [Mask, ...]Returns a list of masks of connected components
Provides a list containing a Mask object for each connected component.
| Parameters: | min (int) -- (optional) indicates the minimum number of bits (to filter out noise) per connected component (default is 0, which equates to no minimum and is equivalent to setting it to 1, as a connected component must have at least 1 bit set) |
|---|---|
| Returns: | a list containing a Mask object for each connected component, an empty list is returned if the mask has no bits set |
| Return type: | list[Mask] |
Note
See connected_component() for details on how a connected component is calculated.
get_bounding_rects() -> [Rect, ...]Returns a list of bounding rects of connected components
Provides a list containing a bounding rect for each connected component.
| Returns: | a list containing a bounding rect for each connected component, an empty list is returned if the mask has no bits set |
|---|---|
| Return type: | list[Rect] |
Note
See connected_component() for details on how a connected component is calculated.
to_surface() -> Surfaceto_surface(surface=None, setsurface=None, unsetsurface=None, setcolor=(255, 255, 255, 255), unsetcolor=(0, 0, 0, 255)) -> SurfaceReturns a surface with the mask drawn on it
Draws this mask on the given surface. Set bits (bits set to 1) and unset bits (bits set to 0) can be drawn onto a surface.
| Parameters: |
|
|---|---|
| Returns: |
the |
| Return type: |
Note
To skip drawing the set bits, both setsurface and setcolor must be None. The setsurface parameter defaults to None, but setcolor defaults to a color value and therefore must be set to None.
Note
To skip drawing the unset bits, both unsetsurface and unsetcolor must be None. The unsetsurface parameter defaults to None, but unsetcolor defaults to a color value and therefore must be set to None.
New in pygame 2.0.0.
© Pygame Developers.
Licensed under the GNU LGPL License version 2.1.
https://www.pygame.org/docs/ref/mask.html